title: Android: tearing effect while calling requestLayout() in OnTouchListener methods? a way to fix?
body: <p>I've tried adding some Drag&amp;Drop mechanism to my project.
  the way I'm doing it is basically making two operations each time a touch event occurs:
  <br>first, updating the parameters I'm using inside onLayout() according to touch event
  <br>second, calling requestLayout() for refreshing.
  <br>(I've tried it both with OnTouchListener methods and with View's onTouchEvent() as in the code to be followed)</p>
  
  <p>problem is, the result on screen wasn't so good.
  it gave the feeling like there is some <strong>tearing</strong> problem with the dragged view(the new drawing starts before the earlier ends)</p>
  
  <p>the code looks something like this (a simplified version):</p>
  
  <pre><code>public class DragAndDrop extends ViewGroup
  {
    View touchView;
    float touchX;
    float touchY;
  
    static int W = 180;
    static int H = 120;
  
    public DragAndDrop(Context context)
    {
      super(context);
  
      touchView = new View(context);
      touchView.setBackgroundColor(0xaaff9900);
      addView(touchView);
    }
  
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
      float width = r - l;
      float height = b - t;
  
      int centerX = (int) (touchX - W / 2);
      int centerY = (int) (touchY - H / 2);
  
      touchView.layout(centerX, centerY, centerX + W, centerY + H);
    }
  
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
      touchX = event.getX();
      touchY = event.getY();
  
      requestLayout();
      return true;
    }
  }
  </code></pre>
  
  <p>after investigating I found the problem is with calling the <strong>requestLayout()</strong> method <strong>inside</strong> the touchListener methods.
  <br>I moved that call to a timer which fired it periodically and the result was much better.</p>
  
  <p>does anyone else experienced that and knows of a better way doing it, without using a timer?
  <br>I prefer to avoid refreshing all the time more then actually needed.</p>
  
  <p>Thanks for your help!</p>