Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Android
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:
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;
                    draw_mod = true;
                case MotionEvent.ACTION_MOVE:                    
                    if (square && draw_mod) {
                    	rect.set((int)mX, (int)mY, 
                                 (int)event.getX(), (int)event.getY());
                    	mCanvas.drawRect(rect, mPaint);
                case MotionEvent.ACTION_UP
                        draw_mod = false;
            return true;
Posted 10-Oct-12 1:47am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can't just do the drawing when you need to register the touch event.
I'm assuming that
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
method of the view that you want to draw on.
protected void onDraw (Canvas canvas)
    //Now draw your rect ontop of whatever was just drawn.
    canvas.drawRect(rect, mPaint);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

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

  Print Answers RSS
0 Zoltán Zörgő 300
1 CHill60 185
2 Peter Leow 170
3 /\jmot 160
4 Pikoh 125
0 Sergey Alexandrovich Kryukov 9,383
1 OriginalGriff 6,881
2 Peter Leow 4,607
3 Zoltán Zörgő 4,284
4 CHill60 2,892

Advertise | Privacy | Mobile
Web04 | 2.8.150129.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100