Im trying to draw a square on a canvas
but only the MotionEvent.ACTION_DOWN
is run, and then the listener
stops.
The idea is when the user touches the screen it get the x1 and y1 locations and every time the onTouch
is fired it must draw the rect frim x1 y1 to the current location, and lastly save the last location in x2 y2 once the user lets go of the screen.
My code looks like this:
surfaceView.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
holder = surfaceView.getHolder();
canvas = holder.lockCanvas();
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
x1=event.getX();
y1=event.getY();
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
x2=event.getX();
y2=event.getY();
}
canvas.drawRect(x1, y1, event.getX(), event.getY(), paint);
holder.unlockCanvasAndPost(canvas);
return true;
}
});
Can anyone give me an example.