user93
user93

Reputation: 1866

Drawing over an image in image view

I have used imageview defined in xml to display .png image i want to draw lines following the finger over the image . I tried setting onTouchListener() over the image view but i dont how to proceed further. Thanks in advance

Upvotes: 0

Views: 282

Answers (1)

Julien Rousseau
Julien Rousseau

Reputation: 985

You are trying the wrong approach.

To draw lines like that you will need to draw on a canvas. You receive such a canvas in an onDraw method.

This means that instead of using a stock ImageView you need to create your own View, override its onDraw method to draw your image and then draw any "lines following the finger over the image". If you want more than the latest line to be visible then you will also need to save those to a canvas that you create so that you will have your onDraw do the following:

  1. Draw your original image

  2. Draw the canvas containing the old lines

  3. Draw the current line.

Given that you will be using your own view you won't use an onTouchListener() but instead override the view's onTouchEvent(MotionEvent event).

Looke into Google's API demos Graphics->Fingerpaint (import it with File -> New -> Other -> Android -> Android Sample Project -> Android X.Y.Z -> API Demos), especially the class com.example.android.apis.graphics.FingerPaint.

Additionally, if you want to draw a point when tapping the screen then this question should be of interest to you.

Upvotes: 3

Related Questions