1. 程式人生 > >android 雙擊動畫點贊效果

android 雙擊動畫點贊效果

1.模仿抖音雙擊點贊效果
public class AnimatorLove extends RelativeLayout {
    private Context mContext;
    float[] num = {-30, -20, 0, 20, 30};//隨機心形圖片角度
    long[] mHits = new long[2];

    public AnimatorLove(Context context) {
        super(context);
        initView(context);
    }

    public AnimatorLove(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public AnimatorLove(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            //每點選一次 實現左移一格資料
            System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
            //給陣列的最後賦當前時鐘值
            mHits[mHits.length - 1] = SystemClock.uptimeMillis();
            //當0出的值大於當前時間-500時  證明在500秒內點選了2次
            if (mHits[0] > SystemClock.uptimeMillis() - 500) {

                final ImageView imageView = new ImageView(mContext);
                LayoutParams params = new LayoutParams(100, 100);
                params.leftMargin = (int) event.getX()-100;
                params.topMargin = (int) event.getY()-100;
                imageView.setImageDrawable(getResources().getDrawable(R.mipmap.icon_like_select));
                imageView.setLayoutParams(params);
                addView(imageView);

                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0))//縮放動畫,X軸2倍縮小至0.9倍
                        .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0))//縮放動畫,Y軸2倍縮小至0.9倍
                        .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)]))//旋轉動畫,隨機旋轉角度num={-30.-20,0,20,30}
                        .with(alpha(imageView, 0, 1, 100, 0))//漸變透明度動畫,透明度從0-1.
                        .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150))//縮放動畫,X軸0.9倍縮小至1倍
                        .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150))//縮放動畫,Y軸0.9倍縮小至1倍
                        .with(translationY(imageView, 0, -600, 800, 400))//平移動畫,Y軸從0向上移動600單位
                        .with(alpha(imageView, 1, 0, 300, 400))//透明度動畫,從1-0
                        .with(scale(imageView, "scaleX", 1, 3f, 700, 400))//縮放動畫,X軸1倍放大至3倍
                        .with(scale(imageView, "scaleY", 1, 3f, 700, 400));//縮放動畫,Y軸1倍放大至3倍

                animatorSet.start();
                animatorSet.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        removeViewInLayout(imageView);
                    }
                });
            }
        }
        return super.onTouchEvent(event);
    }

    public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) {
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) {
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        return rotation;
    }
}
2.雙擊效果 Android sdk給我們提供了GestureDetecto這個類,GestureDetector這個類對外提供了兩個介面:OnGestureListenerOnDoubleTapListener,還有一個內部類SimpleOnGestureListener
public class OnDoubleClick extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDoubleTap(MotionEvent e) { //雙擊 
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) { //單擊 
        return false;
    }
}

3.例項化GestureDetector

GestureDetector mGestureDetector = new GestureDetector(this, new OnDoubleClick());

4.將view的onTouch監聽捕捉到

view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                return mGestureDetector.onTouchEvent(event);
            }
        });