package com.fruit.launcher;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Scroller;

public class ItemAnimate {

	private static final String TAG = "ItemAnimate";

	private Context mContext;
	private float mFromXValue = 0.0f;
    private float mToXValue = 0.0f;
    private float mFromYValue = 0.0f;
    private float mToYValue = 0.0f;

    private int mDuration;
    private ScrollRunnable mScrollRunnable ;
    private View mChild;
    private int mWidth;
    private int mHeight;
    public  boolean animateEnd = true;

    public ItemAnimate(Context context) {
    	mContext = context;
    	mScrollRunnable = new ScrollRunnable();
    }

	public ItemAnimate(float fromXValue, float toXValue, float fromYValue, float toYValue, View child) {
        mFromXValue = fromXValue;
        mToXValue = toXValue;
        mFromYValue = fromYValue;
        mToYValue = toYValue;
        mChild = child;
        mContext = child.getContext();
        mScrollRunnable = new ScrollRunnable();
    }

	public void setAnimateTarget(float fromXValue, float toXValue, float fromYValue, float toYValue, View child) {
		mFromXValue = fromXValue;
        mToXValue = toXValue;
        mFromYValue = fromYValue;
        mToYValue = toYValue;
        mChild = child;
	}

    public void setDuration(int durationMillis) {
        if (durationMillis < 0) {
            throw new IllegalArgumentException("Animation duration cannot be negative");
        }
        mDuration = durationMillis;
    }

    public void setSquare(int width, int height) {
         mWidth = width;
         mHeight = height;
    }

    public void start() {
    	animateEnd = false;
    	Log.d(TAG, "start " + "animateEnd= " + animateEnd +" mFromXValue= " + mFromXValue + " mToXValue= " + mToXValue);
    	mScrollRunnable.startMove((int) Math.abs(mFromXValue - mToXValue),
    			            (int) Math.abs(mFromYValue - mToYValue),mDuration);
    }

    public void stop() {
    	animateEnd = true;
    	Log.d(TAG, "stop " + "animateEnd=" + animateEnd);
    	if (mChild != null) {
    		mChild.removeCallbacks(mScrollRunnable);
    	}
    }

    private class ScrollRunnable implements Runnable {

    	private static final String TAG = "ScrollRunnable";

        private Scroller mScroller;

        int xDirect;
        int yDirect;

        public ScrollRunnable() {
            mScroller = new Scroller(mContext, null);
        }

        public void startMove(int xDistance, int yDistance, int duration) {
            mScroller.startScroll(0, 0, xDistance, yDistance, duration);

            if (mToXValue == mFromXValue) {
            	xDirect = 0;
            } else {
            	xDirect = mToXValue > mFromXValue ? 1 : -1;	
            }

            if (mToYValue == mFromYValue) {
            	yDirect = 0;
            } else {
            	yDirect = mToYValue > mFromYValue ? 1 : -1;
            }

            mChild.post(this);
        }

		@Override
		public void run() {  
			boolean more = mScroller.computeScrollOffset();

			if (more) {
				final int x = mScroller.getCurrX();
				final int y = mScroller.getCurrY();
				int left = (int) (mFromXValue + x * xDirect);
				int top  = (int) (mFromYValue + y * yDirect);
				Log.d(TAG, "left= " + left + " top= " + top);
				mChild.layout(left, top, left + mWidth, top + mHeight);	
				mChild.post(this);
			} else {
				mChild.removeCallbacks(this);
				animateEnd = true;
			}
        }
    }
}
@@ -2517,24 +2517,134 @@
 duration) %7B%0A
+        %09Log.d(TAG,%22startMove,xDistance=%22+xDistance+%22,yDistance=%22+yDistance+%22,duration=%22+duration);%0A        %09%0A
             
@@ -2704,16 +2704,16 @@
 ation);%0A
-
 %0A       
@@ -3024,32 +3024,118 @@
 %0A            %7D%0A%0A
+            Log.d(TAG,%22startMove,xDirect=%22+xDirect+%22,yDirect=%22+yDirect);%0A            %0A
             mChi
@@ -3195,16 +3195,17 @@
 un() %7B  
+%09
 %0A%09%09%09bool
@@ -3247,16 +3247,52 @@
 ffset();
+%0A%09%09%09%0A%09%09%09Log.d(TAG,%22run,more=%22+more);
 %0A%0A%09%09%09if 
@@ -3496,16 +3496,20 @@
 d(TAG, %22
+run 
 left= %22 
@@ -3531,16 +3531,62 @@
  %22 + top
+ + %22 mWidth= %22 + mWidth + %22 mHeight= %22+mHeight
 );%0A%09%09%09%09m

Modify log.