简体   繁体   中英

Android: OnTouch not responding.

I want an action to be performed when the view is touched. However, the touches do not respond. The app doesn't crash, it just seems to ignore it.

public class CustomDrawableView extends View implements OnTouchListener
{
    static final int width = 100;
    static final int height = 50;

    public CustomDrawableView(Context context)
    {
        super(context);
        setFocusable(true);
        setOnTouchListener(mCustomDrawableView);            
        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
         if(event.getAction()==MotionEvent.ACTION_DOWN )
         {

        x = 400;  
        return true;
         }
         else {
             x = 300;
            return false;
        }
    }

    protected void onDraw(Canvas canvas)
    {      
        int mCanvasHeight = canvas.getHeight();
        int mCanvasWidth = canvas.getWidth();
        canvas.save();
        canvas.rotate(R,x,y);


        if (y >= mCanvasHeight-100) {
            y = 0;
        }
        RectF oval = new RectF(x, y, x + width, y
                + height); // set bounds of rectangle
        Paint p = new Paint(); // set some paint options
        p.setColor(Color.BLUE);
        canvas.drawOval(oval, p);
        canvas.restore();

        invalidate();


    }
}

I have tried a bunch of different code to fix it. None of it does anything except if I change setOnTouchListener(mCustomDrawable) to mCustomDrawableView.setOnTouchListener(this) the app crashes. There is a bunch more code in the activity that I did not put up.

I'm guessing you believe that the onTouch() isn't firing because you're not seeing your graphics change in response to changes in x or whatever. If that's the case, it looks like you're missing a call to invalidate() in your touch handler to cause the View to redraw itself again (via a call to onDraw() ).

Also, you have an invalidate() actually inside onDraw() itself which really shouldn't be there. It would certainly cause your View to redraw itself over and over - I suppose - so actually, I guess you should be seeing updates because that's there. But that isn't the way you should make a View animate; you should instead use a Thread or Handler to schedule a regular, periodic invalidate() - or some other means to regularly schedule an update.

Also there should be no need to implement 'OnTouchListener' when you can just override onTouch() as you have done. There are a couple of ways you can detect touch events for a View : (1) Override onTouch() as you have done, to get touch events on that View . (2) Register a listener using setOnTouchListener() . This latter option enables you to have a listener that listens to touch events from one View or multiple View s, and it also 'sees' touch events before a registered View 's own onTouch() sees them.

Another thing I see is that you're setting x to 400 when you get an ACTION_DOWN event but then you're setting it to 300 for any other kind of event such as an ACTION_MOVE. Considering it's actually quite difficult to keep your finger still enough to never cause a string of ACTION_MOVE events immediately after an ACTION_DOWN, perhaps you're just never seeing the graphics when x is at 400 or something.

You really need to post a complete example (for instance your code as you've posted has setOnTouchListener(mCustomDrawableView) where mCustomDrawableView has not been defined anywhere) together with specific errors from Logcat. Also, you should use the debugger to see if your onTouch ever executes (stick a breakpoint in it).

Also, you should accept some answers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM