How can I make a Button more responsive?

后端 未结 4 1845
天涯浪人
天涯浪人 2020-12-03 08:53

I have noticed that some Buttons don\'t seem as responsive as they could be. This applies equally to my app and to most other apps I\'ve tried.

When I press a Button

相关标签:
4条回答
  • 2020-12-03 09:09

    This seems to fix it:

    public boolean onTouchEvent (MotionEvent event) {
       if (event.getAction() == MotionEvent.ACTION_DOWN) setPressed(true);
       return super.onTouchEvent(event);
    }
    

    Still not perfect, but better.

    0 讨论(0)
  • 2020-12-03 09:13

    I have dug into the Android source code to see what was going on.

    It turns out that the android.view.View class (from which Button derives) enters a "PREPRESSED" state before going into the PRESSED state:

    android.view.View:

    1529    /**
    1530      * Indicates a prepressed state;
    1531      * the short time between ACTION_DOWN and recognizing
    1532      * a 'real' press. Prepressed is used to recognize quick taps
    1533      * even when they are shorter than ViewConfiguration.getTapTimeout().
    1534      * 
    1535      * @hide
    1536      */
    1537     private static final int PREPRESSED             = 0x02000000;
    

    android.view.ViewConfiguration.getTapTimeout() is 115 ms on my Nexus One, which is a lot longer than my estimation.

    android.view.ViewConfiguration:

    67     /**
    68      * Defines the duration in milliseconds we will wait to see if a touch event 
    69      * is a tap or a scroll. If the user does not move within this interval, it is
    70      * considered to be a tap. 
    71      */
    72     private static final int TAP_TIMEOUT = 115;
    

    Anyway, from examining View.onTouchEvent is doesn't look like there's a way to avoid this PREPRESSED state by any standard option. Which is a real shame.

    The good news is that I have now verified that the way to avoid this lag is to subclass and override onTouchEvent.

    Thanks for the discussion and answers.

    0 讨论(0)
  • 2020-12-03 09:19

    So what basically happens in any Android application is that the GUI runs in the main thread. If you happen to run some other heavy stuff in the same thread, like communication, video, loops etc. these activities will definitely slow your GUI down. This has happened to my app in the past, then I moved all other processes to a separate thread each and the GUI became really real-time responsive.

    0 讨论(0)
  • 2020-12-03 09:22

    Well... I usually extend Button class and override onTouchEvent method

    public boolean onTouchEvent (MotionEvent event) 
    {
       if (event.getAction() == MotionEvent.ACTION_DOWN) 
       {
          setPressed(true);
       }
       return super.onTouchEvent(event);
    }
    
    0 讨论(0)
提交回复
热议问题