1

Edit : my first part of question is unique, but the second part is similar to some other questions. Main Problem: this is my class code,I use this class to draw lines with custom stroke shape from drawable . I want to draw a line, but it just draw some dots as shown in the picture below. Is there any idea to fix this problem? result

public class MainDrawingView extends View {
    private Bitmap mBitmapBrush;
    private Bitmap rBitmapBrush;
    private Vector2 mBitmapBrushDimensions;
    private List<Vector2> mPositions = new ArrayList<Vector2>(100);

    private static final class Vector2 {
        public Vector2(float x, float y) {
            this.x = x;
            this.y = y;
        }
        public final float x;
        public final float y;
    }

    public MainDrawingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mBitmapBrush = BitmapFactory.decodeResource(context.getResources(), R.drawable.brush01);
        rBitmapBrush = Bitmap.createScaledBitmap(mBitmapBrush, 10, 10, false);
        mBitmapBrushDimensions = new Vector2(rBitmapBrush.getWidth(), rBitmapBrush.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        for (Vector2 pos : mPositions) {
            canvas.drawBitmap(rBitmapBrush, pos.x, pos.y, null);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_MOVE:
                final float posX = event.getX();
                final float posY = event.getY();
                mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
                invalidate();
        }
        return true;
    }
}

Update: I try another class to draw a line with my drawable, but I faced 2 new problem! the first; when I use setShader , there is no any line when touch and move my finger on the screen. and the second;when comment the setShader line in the code, I could draw a simple black line, but if I draw a curved line rapidly, the result is a broken line ! and not a true curve line.so what is your idea to solve these problems?

result2

public class MainDrawingView extends View {
    private Bitmap mBitmapBrush;
    private Bitmap rBitmapBrush;
    private BitmapShader mBitmapShader;
    private Paint paint = new Paint();
    private Path path = new Path();

    public MainDrawingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mBitmapBrush = BitmapFactory.decodeResource(context.getResources(), R.drawable.brush01);
        rBitmapBrush = Bitmap.createScaledBitmap(mBitmapBrush, 10, 10, false);
        mBitmapShader = new BitmapShader(rBitmapBrush, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(5f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        //paint.setShader(mBitmapShader);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY);
                break;
            default:
                return false;
        }
        invalidate();
        return true;
    }
}
X Fa
  • 137
  • 2
  • 9
  • Your question requires some clarification. There are many ways to customize the stroke with `Paint`, including `setShader()`, `setStrokeWidth()`, `setStrokeJoin/Cap/Miter()`, etc. But what exactly do you expect to happen with your bitmap? Should it be stretched? Smeared? Repeated in 1-pixel increments to form a solid line? You should be aware that even when it "works fine", as you say, you are not drawing a line at all. You are simply drawing a series of points that happen to be close enough together to _look like_ a line. This is because `MotionEvents` are discrete, not continuous. – greeble31 Nov 07 '18 at 00:45
  • @greeble31 you right. I want to draw a line with my bitmap, a real line, not repeated dots. Whats the correct way? – X Fa Nov 07 '18 at 07:46
  • It's still not clear what you want. You must be very precise in your description, because this is actually not a common thing to ask for. Maybe look at the [computergraphics stackexchange](https://computergraphics.stackexchange.com/) to try and find the proper term for what it is that you want, or describe a popular photo editing program that implements this feature, and then we can assist you in implementing it. – greeble31 Nov 07 '18 at 13:22
  • @greeble31 if you saw S Note application on samsung notes, there are many options to draw, one of them is pencil. I want draw a line by finger like a pencil effect on the paper. photoshop is famous program, you can chose any brushe to draw a line – X Fa Nov 07 '18 at 13:33
  • Possible duplicate of [Android How to draw a smooth line following your finger](https://stackoverflow.com/questions/8287949/android-how-to-draw-a-smooth-line-following-your-finger) – greeble31 Nov 07 '18 at 13:43
  • @greeble31 thanks,there are some solution for my second problem.I am trying them – X Fa Nov 07 '18 at 13:55
  • See also [this](https://stackoverflow.com/questions/20560322/how-to-draw-path-with-variable-width-in-canvas). By the way, it is better if you can separate each question into an individual post. – greeble31 Nov 07 '18 at 14:24
  • @greeble31 please see this address http://tayasui.com/sketches/ , I want draw lines like its brushes – X Fa Nov 08 '18 at 13:18
  • Such high-quality artistic brushes are likely to be generated by proprietary algorithms. You're not going to find Android API calls that can do that "out of the box." All I can offer is a theoretical treatment: see [here](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.40.2368&rep=rep1&type=pdf). – greeble31 Nov 08 '18 at 14:21

0 Answers0