Skip to content Skip to sidebar Skip to footer

Android: Custom Draw On An Given Image (with SurfaceView)

I am trying to load an image to SurfaceView, and draw some scratch upon this image. To to that, I have create an custom SurfaceView, MySurfaceView, with the following functions: p

Solution 1:

I got it.

It is basically put the draw path to onDraw(Canvas canvas) function as well. To make sure the SurfaceView call onDraw once a path is drew. We need to call invalidate() in on touch event.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(mBitmapScaled, x, y, null);
    if (mPath != null) {
        canvas.drawPath(mPath, mPaint);
    }
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        mPath = new Path();
        mPath.moveTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
        mPath.lineTo(event.getX(), event.getY());
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        mPath.lineTo(event.getX(), event.getY());
    }
    invalidate();
    return true;
}

Or even better, you can change the mPath into an ArrayList, so that you can put more paths on the image.


Post a Comment for "Android: Custom Draw On An Given Image (with SurfaceView)"