0

So this is my current code which uses canvas drawLine, where I'm trying to modify it into drawPath.

My Declared Variables in Main Activity :

ImageView imageResult;
final int RQS_IMAGE1 = 1;

Paint paint;
Uri source;
Bitmap bitmap;
Canvas canvas;
Path path;

My code in On Create method :

       @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawing_board);

    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(10);

    //result is the ID of imageview
    imageResult = findViewById(R.id.result);

    //The user will select Images on gallery
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, RQS_IMAGE1);

Where selected image pass into imageResult variable and then converted into bitmap, and that bitmap will be place into the canvas :

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap tempBitmap;

    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case RQS_IMAGE1:
                source = data.getData();

                try {
                    //tempBitmap is Immutable bitmap,
                    //cannot be passed to Canvas constructor
                    tempBitmap = BitmapFactory.decodeStream(
                            getContentResolver().openInputStream(source));

                    Bitmap.Config config;
                    if (tempBitmap.getConfig() != null) {
                        config = tempBitmap.getConfig();
                    } else {
                        config = Bitmap.Config.ARGB_8888;
                    }

                    //bitmap is Mutable bitmap
                    bitmap = Bitmap.createBitmap(
                            tempBitmap.getWidth(),
                            tempBitmap.getHeight(),
                            config);

                    canvas = new Canvas(bitmap);
                    canvas.drawBitmap(tempBitmap, 0, 0, null);

                    imageResult.setImageBitmap(bitmap);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                break;
        }
    }
}

Now, this is the method where I get the touch points of users finger and put the canvas.drawLine inside of motion events. So then, they can paint on the canvas where the Imageview is placed :

    private float mX,mY;
public void paintOn_Image(){

imageResult.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mX = x;
                mY = y;
           float ratioWidth = (float)bitmap.getWidth()/(float)v.getWidth();
           float ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();

                canvas.drawLine(
                        mX * ratioWidth,
                        mY * ratioHeight,
                        x * ratioWidth,
                        y * ratioHeight,
                        paint);

                imageResult.invalidate();
                break;

            case MotionEvent.ACTION_MOVE:

                ratioWidth = (float) bitmap.getWidth() / (float) v.getWidth();
                ratioHeight = (float)bitmap.getHeight()/(float)v.getHeight();

                canvasMaster.drawLine(
                        mX * ratioWidth,
                        mY * ratioHeight,
                        x * ratioWidth,
                        y * ratioHeight,
                        paint);

                imageResult.invalidate();

                mX = x;
                mY = y;
                break;

            case MotionEvent.ACTION_UP:

                ratioWidth = (float) bitmapMaster.getWidth() / (float) v.getWidth();
                ratioHeight = (float)bitmapMaster.getHeight()/(float)v.getHeight();

                canvasMaster.drawLine(
                        x * ratioWidth,
                        y * ratioHeight,
                        x * ratioWidth,
                        y * ratioHeight,
                        paint);

                imageResult.invalidate();
                break;
        }

        return true;
    }
});

Here's what I've done so far. In the motionEvent.ACTION_DOWN,ACTION_MOVE & ACTION_UP, I change this :

                    canvas.drawLine(
                        mX * ratioWidth,
                        mY * ratioHeight,
                        x * ratioWidth,
                        y * ratioHeight,
                        paint);

                imageResult.invalidate();
                break;

Into this :

                path.moveTo(mX*ratioWidth,mY*ratioHeight);
                path.lineTo(x*ratioWidth,y*ratioHeight);
                path.moveTo(mX*ratioWidth,y*ratioHeight);
                path.lineTo(x*ratioWidth,mY*ratioHeight);
                canvas.drawPath(path,paintDraw);
                imageResult.invalidate();

                canvas.drawPath(path, paint);

                imageResult.invalidate();
                break;

But the application is crashing when the user touch the canvas :(

How can I paint on canvas using Path? Is the calculation incorrect?

The reason why I'm trying to change it into path is we can't undo the drawLine after painting into the canvas. So if you have solution to undo/clear/remove the drawLine, that will also work for me. Thank you.

1 Answers1

0

After reading a lot of documentation and tutorials and informations in this forum.

Just Create DrawableView and CustomImageview instead of dwelling on Imageview.

Sources

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Sumit Sharma May 23 '22 at 06:55