Draw in ImageView

Viewed 20

I want to fill into imageView a path following the finger position and you can't draw outside of imageView. You only able to draw in imageView. I use Paint an on Touch. What I want to do is be able to draw where the user touches that Imageview with an image from the drawable folder.

this my java code

public class MyImageView extends androidx.appcompat.widget.AppCompatImageView {


private ArrayList<Point> mTouches;
private Bitmap mMarker;

//Java constructor
public MyImageView(Context context) {
    super(context);
    init();
}

//XML constructor
public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    mTouches = new ArrayList<Point>();
    mMarker = BitmapFactory.decodeResource(context.getResources(), R.drawable.trois);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    //Capture a reference to each touch for drawing
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        mTouches.add( new Point((int)event.getX(),(int) event.getY()) );
        return true;
    }

    return super.onTouchEvent(event);
}

@Override
protected void onDraw(Canvas c) {
    //Let the image be drawn first
    super.onDraw(c);

    //Draw your custom points here
    Paint paint = new Paint();
    for(Point p : mTouches) {
        c.drawBitmap(mMarker, p.x, p.y, paint);
    }
}

}

Activity code

<ImageView
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/trois" />

I create a main Activity with imageVien instance from the class MyimageView But I get is my original Imageview and it does not do anything when I touch it.

Thanks for helping

0 Answers
Related