点赞动画 飘心

点赞动画

/**
 * 飘心效果自定义View
 * 1.创建ImageView
 * 2.ImageView执行组合动画
 * 3.动画执行完成后销毁View
 * <p>
 * 动画+随机数
 * 位移+缩放+透明度+旋转
 */
public class FlyHeartView extends RelativeLayout {

    private int defaultWidth = 200;
    private long mDuration = 3000;
    private int[] color = {0xFFFF34B3, 0xFF9ACD32, 0xFF9400D3, 0xFFEE9A00};

    public FlyHeartView(Context context) {
        super(context);
        initFrameLayout();
    }

    public FlyHeartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFrameLayout();
    }


    private void initFrameLayout() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(defaultWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
        setLayoutParams(params);
    }

    /**
     * 创建心形的视图
     */
    private ImageView createHeartView() {
        ImageView heartIv = new ImageView(getContext());
        LayoutParams params = new LayoutParams(defaultWidth / 2, defaultWidth / 2);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);

        heartIv.setLayoutParams(params);
        heartIv.setImageResource(R.mipmap.ic_heart);
        //改变颜色
        heartIv.setImageTintList(ColorStateList.valueOf(color[(int) (color.length * Math.random())]));
        return heartIv;
    }

    /**
     * 执行动画
     */
    public void startFly() {
        final ImageView heartIv = createHeartView();
        addView(heartIv);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(createTranslationX(heartIv))
                .with(createTranslationY(heartIv))
                .with(createScale(heartIv))
                .with(createRotation(heartIv))
                .with(createAlpha(heartIv));
        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(heartIv);
            }
        });
    }

    /**
     * 横向正弦位移动画
     *
     * @return 属性动画
     */
    private Animator createTranslationX(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, (float) (defaultWidth * Math.random() / 4));
        animator.setDuration(mDuration);
        //CycleInterpolator cycles 正弦曲线数
        animator.setInterpolator(new CycleInterpolator((float) (3 * Math.random())));
        return animator;
    }

    /**
     * 横向正弦位移动画
     *
     * @return 属性动画
     */
    private Animator createTranslationY(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, -1000);
        animator.setDuration(mDuration);
        animator.setInterpolator(new AccelerateInterpolator());
        return animator;
    }

    /**
     * 加速放大动画
     *
     * @return 属性动画
     */
    private Animator createScale(View view) {
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1, 1.5f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1, 1.5f);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(mDuration);
        set.setInterpolator(new AccelerateInterpolator());
        set.play(animatorX).with(animatorY);
        return set;
    }

    /**
     * 透明动画
     *
     * @return 属性动画
     */
    private Animator createAlpha(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1, 0.1f);
        animator.setDuration(mDuration);
        animator.setInterpolator(new AccelerateInterpolator());
        return animator;
    }

    /**
     * 旋转动画
     *
     * @return 属性动画
     */
    private Animator createRotation(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0, (float) (25f * Math.random()));
        animator.setDuration(mDuration);
        animator.setInterpolator(new CycleInterpolator((float) (6 * Math.random())));
        return animator;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值