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);
            }
        });

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值