自定义View-31 直播点赞效果

1. 效果

ghdd1-co0e7.gif

2. 分析

2.1 在界面底部中间出现爱心图片

2.1.1 放大和透明度变化的动画

2.2 沿 s 型的移动

2.2.1三阶贝塞尔曲线

image.png

3. 代码实现

public class LoveLayout extends RelativeLayout {
    private static final String TAG = "zsjTAG";
    private int[] mLoveImages;
    private Random mRandom;
    private Interpolator[] mInterpolator;

    /**
     * 控件的宽高
     */
    private int mHeight, mWidth;
    private int mLoveImageHeight, mLoveImageWidth;

    public LoveLayout(Context context) {
        this(context, null);
    }

    public LoveLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoveLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRandom = new Random();
        mLoveImages = new int[]{
                R.drawable.pl_blue, R.drawable.pl_red, R.drawable.pl_yellow
        };

        Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.pl_blue);
        mLoveImageHeight = drawable.getIntrinsicHeight();
        mLoveImageWidth = drawable.getIntrinsicWidth();


        mInterpolator = new Interpolator[]{new AccelerateDecelerateInterpolator(), new AccelerateInterpolator(),
                new DecelerateInterpolator(), new LinearInterpolator()};
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    public void addLove() {
        //在底部中心出现爱心的图片
        ImageView loveIv = new ImageView(getContext());
        loveIv.setImageResource(mLoveImages[mRandom.nextInt(mLoveImages.length)]);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM);
        layoutParams.addRule(CENTER_HORIZONTAL);
        loveIv.setLayoutParams(layoutParams);
        addView(loveIv);

        AnimatorSet animationSet = getAnimationSet(loveIv);
        animationSet.start();
    }

    private AnimatorSet getAnimationSet(ImageView loveIv) {
        //动画,一个是放大动画,透明度动画
        AnimatorSet animatorSet = new AnimatorSet();
        // 放大动画
        AnimatorSet innerAnimatorSet = new AnimatorSet();
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(loveIv, "alpha", 0.2f, 1.0f);
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(loveIv, "scaleX", 0.2f, 1.0f);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(loveIv, "scaleY", 0.2f, 1.0f);

        innerAnimatorSet.setDuration(350);
        innerAnimatorSet.playTogether(alphaAnimator, scaleXAnimator, scaleYAnimator);
        //贝塞尔曲线动画
        animatorSet.playSequentially(innerAnimatorSet, getBezierAnimator(loveIv));

        return animatorSet;

    }

    private Animator getBezierAnimator(final ImageView loveIv) {
        //固定,在最低的中间
        PointF pointF0 = new PointF(mWidth / 2 - mLoveImageWidth / 2, mHeight - mLoveImageHeight);
        //确保 p2 的y 值一定要小于 p1 的y值.
        PointF pointF1 = new PointF(mRandom.nextInt(mWidth) - mLoveImageWidth, mRandom.nextInt(mHeight / 2) + mHeight / 2);
        PointF pointF2 = new PointF(mRandom.nextInt(mWidth) - mLoveImageWidth, mRandom.nextInt(mHeight / 2));
        //y是固定的.x的宽度的范围
        PointF pointF3 = new PointF(mRandom.nextInt(mWidth) - mLoveImageWidth, 0);
        BezierTypeEvaluator bezierTypeEvaluator = new BezierTypeEvaluator(pointF1, pointF2);
        ValueAnimator valueAnimator = ObjectAnimator.ofObject(bezierTypeEvaluator, pointF0, pointF3);
        valueAnimator.setDuration(3000);
        //设置随机插值器
        valueAnimator.setInterpolator(mInterpolator[mRandom.nextInt(mInterpolator.length)]);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                loveIv.setX(pointF.x);
                loveIv.setY(pointF.y);

                float fraction = animation.getAnimatedFraction();
                loveIv.setAlpha(1 - fraction + 0.2f);
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                removeView(loveIv);
            }
        });
        return valueAnimator;
    }
}

public class BezierTypeEvaluator implements TypeEvaluator<PointF> {

    private PointF mPointF1;
    private PointF mPointF2;

    public BezierTypeEvaluator(PointF pointF1, PointF pointF2) {
        mPointF1 = pointF1;
        mPointF2 = pointF2;
    }

    @Override
    public PointF evaluate(float t, PointF point0, PointF point3) {
        PointF pointF = new PointF();
        pointF.x = point0.x * (1 - t) * (1 - t) * (1 - t) + 3 * mPointF1.x * t * (1 - t) * (1 - t)
                + 3 * mPointF2.x * t * t * (1 - t) + point3.x * t * t * t;
        pointF.y = point0.y * (1 - t) * (1 - t) * (1 - t) + 3 * mPointF1.y * t * (1 - t) * (1 - t)
                + 3 * mPointF2.y * t * t * (1 - t) + point3.y * t * t * t;
        return pointF;
    }
}

4.完整代码

lovelayout

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,037评论 3 119
  • 她说要嫁给感动自己的人 他做了很多 却难以感动一颗柔弱的心 他想找一个有能力保护的人 所以努力工作认真生活 只为了...
    阿仁Cowboy阅读 2,635评论 0 1
  • 其实你始终很孤独 但还是对生活充满热情 其实你始终坚持一些梦想 也明明知道有些梦不可能实现 其实你始终相信爱情 也...
    时光慢递阅读 2,755评论 0 1
  • 钟葳,生日快乐。 钟葳,薇薇一笑很倾城哈哈哈哈哈哈。 文字什么的我还真写不来。随便讲讲吧,...
    217238444a9d阅读 2,047评论 0 1
  • 自古以来 江湖中都流传着一个可怕的传说 那就是 “胖人先胖脸,瘦人先瘦胸” 本来打算跑步减肥的你 结果越跑胸越小 ...
    卖奶昔减肥的百万富翁阅读 5,598评论 0 0