Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Moving a View with onTouch has definite lag
    text
    copied!<p>I'm coming up the learning curve on Android, at the moment working on user interface details. One of the basic things I am trying is moving something around on the screen with my finger. Seems like a very fundamental thing to do.</p> <p>I started with a RelativeLayout (RL), then created a 100x100 View that has a contrasting background color (intentionally simple, low graphics overhead) and add that View to the RL. Using View.setX and View.setY I can move the View around just fine.</p> <p>Next, I added an onTouchListener to the View and used the resulting .getRawX and .getRawY values to move the View. It works - but there is a definite lag. I expected the View to stay right under my finger but it trails behind. It's very noticeable.</p> <p>EDIT: Here's my onTouch code:</p> <pre><code> public boolean onTouch(View v,MotionEvent event) { boolean bExitValue = true; int iAction; iAction = event.getActionMasked(); if (MotionEvent.ACTION_MOVE == iAction) { v.setX(event.getRawX()); v.setY(event.getRawY()); } else if (MotionEvent.ACTION_DOWN != iAction) { bExitValue = false; } return(bExitValue); } </code></pre> <p>I've tightened up the onTouch method as much as possible to keep it efficient - I'm only using primitives, no objects are being created, etc. - but that didn't help. Further things I've tried:</p> <ul> <li><p>Reducing the size of the View from 100x100 to 50x50, thus dropping the number of pixels that must be redrawn by 75%. No improvement.</p></li> <li><p>Overrode View.onTouchEvent instead of registering an onTouchListener. No improvement.</p></li> <li><p>Added android:hardwareAccelerated="true" to the manifest. No improvement.</p></li> <li><p>It seems counterintuitive, but I tried inserting a Thread.sleep() in the MotionEvent handler, based on various comments I found online that suggested doing so. No improvement, but definitely got the predictable slowdown as the sleep value increased!</p></li> <li><p>Used MotionEvent.getx and .getY instead of the "raw" versions. Got interleaved pairs of coordinates that appeared to be absolute and relative, making the View jumpy, so I abandoned that for now but will research when this current question is resolved.</p></li> </ul> <p>I'm running my code on a new Google Nexus 7, so I don't think the hardware is starving for throughput.</p> <p>Moving an object on the screen with your finger is really fundamental. I've got to be missing something, because no one would ship commercial code with the slow reaction time I'm seeing.</p> <p>I've scanned here, and everything Google can find, but no solutions yet. Thanks in advance for any guidance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload