Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The problem is that the square draws all the time, and I want to draw as we let go of finger. I want to see the square, but I don't know how to resolve it, do you have any suggestions?

This is my code:
C#
@Override
public boolean onTouchEvent(MotionEvent event)
{
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
                    if (square) {
                    	rect = new Rect();
                    	mX = x;
                        mY = y;
                	invalidate();
		    }
                    draw_mod = true;
                    break;
                case MotionEvent.ACTION_MOVE:                    
                    if (square && draw_mod) {
                    	rect.set((int)mX, (int)mY, 
                                 (int)event.getX(), (int)event.getY());
                    	mCanvas.drawRect(rect, mPaint);
                	invalidate();
		     }
                     break;
                case MotionEvent.ACTION_UP
                        draw_mod = false;
                	break;
            }
            return true;
        }:
Posted

You can't just do the drawing when you need to register the touch event.

I'm assuming that
rect
is a member variable, but it doesn't follow the naming convention like your other variables. If it isn't a private member variable, then make it one.

You will need to override the
onDraw()
method of the view that you want to draw on.

Java
protected void onDraw (Canvas canvas)
{
    super.onDraw(canvas);

    //Now draw your rect ontop of whatever was just drawn.
    canvas.drawRect(rect, mPaint);
}
 
Share this answer
 
v2

Uploaded with ImageShack.us

Most of works, only I have problem with drawing. I do not want this effect as the picture.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900