自定義View-31 直播點贊效果

1. 效果

ghdd1-co0e7.gif

2. 分析

2.1 在界面底部中間出現(xiàn)愛心圖片

2.1.1 放大和透明度變化的動畫

2.2 沿 s 型的移動

2.2.1三階貝塞爾曲線

image.png

3. 代碼實現(xiàn)

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() {
        //在底部中心出現(xiàn)愛心的圖片
        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);
        //設(shè)置隨機插值器
        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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 她說要嫁給感動自己的人 他做了很多 卻難以感動一顆柔弱的心 他想找一個有能力保護的人 所以努力工作認真生活 只為了...
    阿仁Cowboy閱讀 380評論 0 1
  • 其實你始終很孤獨 但還是對生活充滿熱情 其實你始終堅持一些夢想 也明明知道有些夢不可能實現(xiàn) 其實你始終相信愛情 也...
    時光慢遞閱讀 386評論 0 1
  • 鐘葳,生日快樂。 鐘葳,薇薇一笑很傾城哈哈哈哈哈哈。 文字什么的我還真寫不來。隨便講講吧,...
    217238444a9d閱讀 329評論 0 1
  • 自古以來 江湖中都流傳著一個可怕的傳說 那就是 “胖人先胖臉,瘦人先瘦胸” 本來打算跑步減肥的你 結(jié)果越跑胸越小 ...
    賣奶昔減肥的百萬富翁閱讀 1,780評論 0 0

友情鏈接更多精彩內(nèi)容