drag the red rectangle by move the mouse in Android?
<p>i put the mouse in  the  red rectangle,when i drag the mouse,i hope the rectangle also move followed the mouse,when the mouse not in the rectangle,i hope the rectangle not move,but my code not move smooth,or sometimes not move,can you give some advice to find the mistake?</p>

<pre><code>    public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    private static final int LineLength=30;
    List&lt;Point&gt; points = new ArrayList&lt;Point&gt;();
    Paint paint = new Paint();
    float locationX,locationY;
    private float mLastTouchX;
    private float mLastTouchY;
    //rectangle center
    private float mPosX;
    private float mPosY;

    private float mPosX1;
    private float mPosY1;
    Bitmap bitmap,bmp,xiao;

    //the width and height of the screen
    int  screenWidth ,screenHeight;

    int xLength;

    boolean isFirst=true;
    boolean  isLeft=false;

    Rect r,rBig;

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);


        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);   
        screenHeight = wm.getDefaultDisplay().getHeight();   
        screenWidth= wm.getDefaultDisplay().getWidth();   
        mPosX=screenWidth/2;
        mPosY=screenHeight/2;



         paint.setColor(Color.RED);
         paint.setAntiAlias(true);
        // meinv is the big pic 
          bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
       // icon is system icon.png
          bmp=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
      // msn_protocol is 20*20 very small
          xiao=BitmapFactory.decodeResource(getResources(), R.drawable.msn_protocol);

          xLength=(int) Math.hypot(xiao.getWidth(), xiao.getHeight());

           r= new Rect();
           r.set((int)mPosX-LineLength-xiao.getWidth(), (int)mPosY-LineLength-xiao.getHeight(), (int)mPosX-LineLength, (int)mPosY-LineLength);
           rBig=new Rect();
           rBig.set((int)mPosX-LineLength,(int)mPosY-LineLength,(int)mPosX+LineLength,(int)mPosY+LineLength);
    }

    @Override
    public void onDraw(Canvas canvas) {

          canvas.save();

          canvas.drawBitmap(bitmap, 0, 0,null);

            canvas.drawBitmap(xiao, mPosX-LineLength-xiao.getWidth(), mPosY-LineLength-xiao.getHeight(),null);


            canvas.drawLine(mPosX-LineLength, mPosY-LineLength-xiao.getHeight()/2, mPosX+LineLength, mPosY-LineLength-xiao.getHeight()/2, paint);


            canvas.drawLine(mPosX-LineLength-xiao.getWidth()/2, mPosY-LineLength, mPosX-LineLength-xiao.getWidth()/2, mPosY+LineLength, paint);


            canvas.drawBitmap(xiao, mPosX+LineLength, mPosY-LineLength-xiao.getHeight(),null);


            canvas.drawBitmap(xiao, mPosX-LineLength-xiao.getWidth(), mPosY+LineLength,null);

            canvas.drawBitmap(xiao, mPosX+LineLength, mPosY+LineLength,null);

            canvas.drawLine(mPosX+LineLength+xiao.getWidth()/2, mPosY-LineLength, mPosX+LineLength+xiao.getWidth()/2, mPosY+LineLength, paint);

            canvas.drawLine(mPosX-LineLength, mPosY+LineLength+xiao.getHeight()/2, mPosX+LineLength, mPosY+LineLength+xiao.getHeight()/2, paint);

           canvas.restore();
           if(isLeft)
          {
              canvas.save();



               Matrix matrix = new Matrix();
                matrix.preScale(0.2f, 0.2f);
               Bitmap rotatedBitmap = Bitmap.createBitmap(bmp,0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
             canvas.drawBitmap(rotatedBitmap, mPosX-LineLength, mPosY-LineLength,null);
              canvas.restore();
          }



    }

    public boolean onTouch(View view, MotionEvent event) {

         isFirst=false;
         switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN: {
             mPosX1 = event.getX();
             mPosY1 = event.getY();
              mLastTouchX=mPosX1;
              mLastTouchY=mPosX1;

              if(r.contains( (int)mLastTouchX, (int)mLastTouchY))
              {
                  isLeft=true;
                  invalidate();
              }


             break;
         }

         case MotionEvent.ACTION_MOVE: {

             final float x = event.getX();
             final float y = event.getY();

             // Only move if the ScaleGestureDetector isn't processing a gesture.

                 final float dx = x - mLastTouchX;
                 final float dy = y - mLastTouchY;

                 mPosX1 += dx;
                 mPosY1 += dy;
                 mLastTouchX = x;
                 mLastTouchY = y;
               if(rBig.contains((int)x,(int)y))

               {
                   mPosX += dx;
                   mPosY += dy;
                   invalidate();
               }





             break;
         }

         case MotionEvent.ACTION_UP: {

             break;
         }

         case MotionEvent.ACTION_CANCEL: {

             break;
         }

    }
         return  true;
}


}
</code></pre>
