Backlight87
8/31/2017 - 9:26 PM

/** *弹性动画SpringAnimation的使用 * 基本操作都在touch事件里面 * 1、基本用法就是通过对view设置旋转或者平移 * 2、new 一个spring对象出来设置他的相关属性,start即可,详细的请参看demo45行左右有设置的详细介绍 *

/** *弹性动画SpringAnimation的使用

  • 基本操作都在touch事件里面
  • 1、基本用法就是通过对view设置旋转或者平移
  • 2、new 一个spring对象出来设置他的相关属性,start即可,详细的请参看demo45行左右有设置的详细介绍
  • 3、取消动画同样是cancel方法即可 */ 不过缺点:弹簧动画他是会来回弹的,所以像上拉加载更多的那种上拉弹动暂时做不到
/**
 *弹性动画SpringAnimation的使用
 * 基本操作都在touch事件里面
 * 1、基本用法就是通过对view设置旋转或者平移
 * 2、new 一个spring对象出来设置他的相关属性,start即可,详细的请参看demo45行左右有设置的详细介绍
 * 3、取消动画同样是cancel方法即可
 */
public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private VelocityTracker mVelocityTracker;
    private int id;
    private TextView mInfo;
    private SpringAnimation mSpringAnimationX, mSpringAnimationY;
    private float x,y,x0,y0;
    int count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInfo = new TextView(this);
        mInfo.setLines(4);
        mInfo.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        mInfo.setTextColor(Color.BLACK);
        mInfo.setBackgroundColor(Color.BLUE);
        setContentView(mInfo);
        mVelocityTracker = VelocityTracker.obtain();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        count++;
        Log.e(TAG,"我滑动一次调用第几次的touch:"+count);
        mVelocityTracker.addMovement(event);
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                //获取触点的ID,因为可能有多个。0代表第一个
                id = event.getPointerId(0);
                x0=event.getX();
                y0=event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                mVelocityTracker.computeCurrentVelocity(1000);
                float vX = mVelocityTracker.getXVelocity(id);
                float vY = mVelocityTracker.getYVelocity(id);
                mInfo.setTranslationX(event.getX(id)-x0);
                mInfo.setTranslationY(event.getY(id)-y0);
                mInfo.setText("顺时速度x   " + vX + "顺时速度y   " + vY);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if(mInfo.getTranslationX()!=0){
                    //三个参数分别是要动画的view,动画名称平移,finalposition 最终的位置
                    mSpringAnimationX=new SpringAnimation(mInfo,SpringAnimation.TRANSLATION_X,0);
                    //设置阻尼系数
                    mSpringAnimationX.getSpring().setDampingRatio(0.1f);
                    //设置劲度系数
                    mSpringAnimationX.getSpring().setStiffness(0.1f);
                    mSpringAnimationX.setStartVelocity(mVelocityTracker.getXVelocity(id));
                    mSpringAnimationX.start();
                }
                if(mInfo.getTranslationY()!=0){
                    mSpringAnimationY=new SpringAnimation(mInfo,SpringAnimation.TRANSLATION_Y,0);
                    mSpringAnimationY.setStartVelocity(mVelocityTracker.getYVelocity(id));
                    mSpringAnimationY.start();
                }
                break;
        }

        return super.onTouchEvent(event);
    }

    //释放VelocityTracker

    private void releaseVelocityTracker() {
        if (null != mVelocityTracker) {
            mVelocityTracker.clear();
            mVelocityTracker.recycle();
            mVelocityTracker = null;
        }
    }
}