仿抖音双击点赞效果

/**

* 创建者    yf

* 创建时间  2018/8/17 11:51

* 描述       ${TODO}

*/

public class PraiseLayout extends FrameLayout{

    //点击次数

    private int count = 0;

    //第一次点击时间

    private long firstClick = 0;

    //第二次点击时间

    private long secondClick = 0;

    /**

    * 两次点击时间间隔,单位毫秒

    */

    private final int totalTime = 300;

    Drawable drawable;

    public PraiseLayout(@NonNull Context context) {

        super(context);

        Drawable var10001 = this.getResources().getDrawable(R.mipmap.ic_heart);

        this.drawable = var10001;

        this.setClipChildren(false);

    }

    public PraiseLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);

        Drawable var10001 = this.getResources().getDrawable(R.mipmap.ic_heart);

        this.drawable = var10001;

        this.setClipChildren(false);

    }

    public PraiseLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

        Drawable var10001 = this.getResources().getDrawable(R.mipmap.ic_heart);

        this.drawable = var10001;

        this.setClipChildren(false);

    }

    @Override

    public boolean onTouchEvent(MotionEvent event) {

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

            count++;

            if (1 == count) {

                //记录第一次点击时间

                firstClick = System.currentTimeMillis();

            } else if (2 == count) {

                //记录第二次点击时间

                secondClick = System.currentTimeMillis();

                //判断二次点击时间间隔是否在设定的间隔时间之内

                if (secondClick - firstClick < totalTime) {

                    float x = event.getX();

                    float y = event.getY();

                    addHeartView(x,y);

                    count = 0;

                    firstClick = 0;

                } else {

                    firstClick = secondClick;

                    count = 1;

                }

                secondClick = 0;

            }

        }

        return super.onTouchEvent(event);

    }

    private void addHeartView(float x, float y) {

        LayoutParams lp = new LayoutParams(this.drawable.getIntrinsicWidth(), this.drawable.getIntrinsicHeight());

        lp.leftMargin = (int)(x - (float)(this.drawable.getIntrinsicWidth() / 2));

        lp.topMargin = (int)(y - (float)this.drawable.getIntrinsicHeight());

        final ImageView img = new ImageView(this.getContext());

        img.setScaleType(ImageView.ScaleType.MATRIX);

        Matrix matrix = new Matrix();

        matrix.postRotate(this.getRandomRotate());

        img.setImageMatrix(matrix);

        img.setImageDrawable(this.drawable);

        img.setLayoutParams((android.view.ViewGroup.LayoutParams)lp);

        this.addView((View)img);

        AnimatorSet animSet = this.getShowAnimSet(img);

        final AnimatorSet hideSet = this.getHideAnimSet(img);

        animSet.start();

        animSet.addListener((new AnimatorListenerAdapter() {

            @Override

            public void onAnimationEnd(@Nullable Animator animation) {

                super.onAnimationEnd(animation);

                hideSet.start();

            }

        }));

        hideSet.addListener((new AnimatorListenerAdapter() {

            @Override

            public void onAnimationEnd(@Nullable Animator animation) {

                super.onAnimationEnd(animation);

                PraiseLayout.this.removeView(img);

            }

        }));

    }

    private AnimatorSet getHideAnimSet(ImageView view) {

        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", new float[]{1.0F, 0.1F});

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", new float[]{1.0F, 2.0F});

        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", new float[]{1.0F, 2.0F});

        ObjectAnimator translation = ObjectAnimator.ofFloat(view, "translationY", new float[]{0.0F, -150.0F});

        AnimatorSet animSet = new AnimatorSet();

        animSet.playTogether(new Animator[]{alpha, scaleX, scaleY, translation});

        animSet.setDuration(500);

        return animSet;

    }

    private AnimatorSet getShowAnimSet(ImageView imageView) {

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1.2F, 1.0F});

        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", new float[]{1.2F, 1.0F});

        AnimatorSet animSet = new AnimatorSet();

        animSet.playTogether(new Animator[]{scaleX, scaleY});

        animSet.setDuration(100);

        return animSet;

    }

    private final float getRandomRotate() {

        return (float)((new Random()).nextInt(20) - 10);

    }

}

你可能感兴趣的:(仿抖音双击点赞效果)