1. 程式人生 > >雙擊飛小心心-仿抖音點贊

雙擊飛小心心-仿抖音點贊

class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override

        public boolean onDoubleTap(MotionEvent e) {

            addLoveView(e);

            return super.onDoubleTap(e);

        }

    }

private int x1;

private int y1;

private void addLoveView(MotionEvent e) {

float x =e.getRawX();

    float y =e.getRawY();

    if (x <100) {

x1 =102;

    }else {

x1 = (int)x;

    }

if (y <100) {

y1 =102;

    }else {

y1 = (int)y;

    }

if (mLove !=null) {

mLove.addLoveView(x1, y1);

    }

}

gestureDetector = new GestureDetector(getContext(), new MyOnGestureListener());

@Override

public boolean onTouchEvent(MotionEvent event) {

gestureDetector.onTouchEvent(event);

return true;

}

自定義RelativeLayout

public class Love extends RelativeLayout {

    private Context context;

    private LayoutParams params;

    private Drawable[] icons = new Drawable[4];

    private Interpolator[] interpolators = new Interpolator[4];

    private int mWidth;

    private int mHeight;

    public Love(Context context, AttributeSet attrs) {

        super(context, attrs);

        this.context = context;

        initView();

    }

    private void initView() {

        // 圖片資源

        icons[0] = getResources().getDrawable(R.drawable.heart_red);

        icons[1] = getResources().getDrawable(R.drawable.heart_red);

        icons[2] = getResources().getDrawable(R.drawable.heart_red);

        icons[3] = getResources().getDrawable(R.drawable.heart_red);

        // 插值器

        interpolators[0] = new AccelerateDecelerateInterpolator(); // 在動畫開始與結束的地方速率改變比較慢,在中間的時候加速

        interpolators[1] = new AccelerateInterpolator();  // 在動畫開始的地方速率改變比較慢,然後開始加速

        interpolators[2] = new DecelerateInterpolator(); // 在動畫開始的地方快然後慢

        interpolators[3] = new LinearInterpolator();  // 以常量速率改變

    }

    public void addLoveView(float x, float y) {

        mWidth = (int) (x - 100);

        mHeight = (int) (y - 100);

        final ImageView iv = new ImageView(context);

        params = new LayoutParams(200, 200);

        iv.setLayoutParams(params);

        iv.setImageDrawable(icons[new Random().nextInt(4)]);

        addView(iv);

        // 開啟動畫,並且用完銷燬

        AnimatorSet set = getAnimatorSet(iv);

        set.start();

        set.addListener(new AnimatorListenerAdapter() {

            @Override

            public void onAnimationEnd(Animator animation) {

                // TODO Auto-generated method stub

                super.onAnimationEnd(animation);

                removeView(iv);

            }

        });

    }

    /**

    * 獲取動畫集合

    *

    * @param iv

    */

    private AnimatorSet getAnimatorSet(ImageView iv) {

        // 1.alpha動畫

        ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0.3f, 1f);

        // 2.縮放動畫

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 0.5f, 1.5f);

        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0.5f, 1.5f);

        // 動畫集合

        AnimatorSet set = new AnimatorSet();

        set.playTogether(alpha, scaleX, scaleY);

        set.setDuration(2000);

        // 貝塞爾曲線動畫

        ValueAnimator bzier = getBzierAnimator(iv);

        AnimatorSet set2 = new AnimatorSet();

        set2.playTogether(set, bzier);

        set2.setTarget(iv);

        return set2;

    }

    /**

    * 貝塞爾動畫

    */

    private ValueAnimator getBzierAnimator(final ImageView iv) {

        // TODO Auto-generated method stub

        PointF[] PointFs = getPointFs(iv); // 4個點的座標

        BasEvaluator evaluator = new BasEvaluator(PointFs[1], PointFs[2]);

        ValueAnimator valueAnim = ValueAnimator.ofObject(evaluator, PointFs[0], PointFs[3]);

        valueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override

            public void onAnimationUpdate(ValueAnimator animation) {

                // TODO Auto-generated method stub

                PointF p = (PointF) animation.getAnimatedValue();

                iv.setX(p.x);

                iv.setY(p.y);

                iv.setAlpha(1 - animation.getAnimatedFraction()); // 透明度

            }

        });

        valueAnim.setTarget(iv);

        valueAnim.setDuration(2000);

        valueAnim.setInterpolator(interpolators[new Random().nextInt(4)]);

        return valueAnim;

    }

    private PointF[] getPointFs(ImageView iv) {

        // TODO Auto-generated method stub

        PointF[] PointFs = new PointF[4];

        PointFs[0] = new PointF(); // p0

        PointFs[0].x = ((int) mWidth);

        PointFs[0].y = mHeight;

        PointFs[1] = new PointF(); // p1

        PointFs[1].x = new Random().nextInt(mWidth);

        PointFs[1].y = new Random().nextInt(mHeight / 2) + mHeight / 2 + params.height;

        PointFs[2] = new PointF(); // p2

        PointFs[2].x = new Random().nextInt(mWidth);

        PointFs[2].y = new Random().nextInt(mHeight / 2);

        PointFs[3] = new PointF(); // p3

        PointFs[3].x = new Random().nextInt(mWidth);

        PointFs[3].y = 0;

        return PointFs;

    }

}