美文网首页
点赞动画

点赞动画

作者: 名字不想带英文 | 来源:发表于2020-03-30 15:51 被阅读0次

先上效果图


thumb.gif
import android.animation.TypeEvaluator;
import android.graphics.PointF;

public class BezierEvaluator implements TypeEvaluator<PointF> {

    private PointF pointF1;
    private PointF pointF2;

    public BezierEvaluator(PointF pointF1, PointF pointF2) {
        this.pointF1 = pointF1;
        this.pointF2 = pointF2;
    }

    @Override
    public PointF evaluate(float time, PointF startValue, PointF endValue) {

        float timeLeft = 1.0f - time;
        PointF point = new PointF();// 结果

        point.x = timeLeft * timeLeft * timeLeft * (startValue.x) + 3
                * timeLeft * timeLeft * time * (pointF1.x) + 3 * timeLeft
                * time * time * (pointF2.x) + time * time * time * (endValue.x);

        point.y = timeLeft * timeLeft * timeLeft * (startValue.y) + 3
                * timeLeft * timeLeft * time * (pointF1.y) + 3 * timeLeft
                * time * time * (pointF2.y) + time * time * time * (endValue.y);
        return point;
    }
}
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.eluton.medclass.R;

import java.util.Random;

public class PeriscopeLayout extends RelativeLayout {

    private Interpolator line = new LinearInterpolator();// 线性
    private Interpolator acc = new AccelerateInterpolator();// 加速
    private Interpolator dce = new DecelerateInterpolator();// 减速
    private Interpolator accdec = new AccelerateDecelerateInterpolator();// 先加速后减速
    private Interpolator[] interpolators;

    private int mHeight;
    private int mWidth;
    private LayoutParams lp;
    private Drawable[] drawables;
    private Random random = new Random();

    private int dHeight;
    private int dWidth;

    public PeriscopeLayout(Context context) {
        super(context);
        init();
    }

    public PeriscopeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PeriscopeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public PeriscopeLayout(Context context, AttributeSet attrs,
                           int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {

        // 初始化显示的图片
        drawables = new Drawable[5];
        Drawable praise1 = getResources().getDrawable(R.mipmap.praise1);
        Drawable praise2 = getResources().getDrawable(R.mipmap.praise2);
        Drawable praise3 = getResources().getDrawable(R.mipmap.praise3);
        Drawable praise4 = getResources().getDrawable(R.mipmap.praise4);
        Drawable praise5 = getResources().getDrawable(R.mipmap.praise5);

        drawables[0] = praise1;
        drawables[1] = praise2;
        drawables[2] = praise3;
        drawables[3] = praise4;
        drawables[4] = praise5;

        // 初始化插补器
        interpolators = new Interpolator[4];
        interpolators[0] = line;
        interpolators[1] = acc;
        interpolators[2] = dce;
        interpolators[3] = accdec;

    }

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

//    点赞
    public void addHeart() {
        if (mWidth > 0) {
            ImageView imageView = new ImageView(getContext());
            // 随机选一个
            Drawable result=drawables[random.nextInt(5)];
            // 获取图的宽高 用于后面的计算
            // 注意 我这里3张图片的大小都是一样的,所以我只取了一个
            dHeight = result.getIntrinsicHeight();
            dWidth = result.getIntrinsicWidth();

            // 底部 并且 水平居中
            lp = new LayoutParams(dWidth, dHeight);
            lp.addRule(CENTER_HORIZONTAL, TRUE);// 这里的TRUE 要注意 不是true
            lp.addRule(ALIGN_PARENT_BOTTOM, TRUE);

            imageView.setImageDrawable(result);
            imageView.setLayoutParams(lp);

            addView(imageView);

            Animator set = getAnimator(imageView);
            set.addListener(new AnimEndListener(imageView));
            set.start();
        }
    }

    private Animator getAnimator(View target) {
//        AnimatorSet set = getEnterAnimtor(target);

        ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);

        AnimatorSet finalSet = new AnimatorSet();
        finalSet.playSequentially(bezierValueAnimator);
//        finalSet.playSequentially(set, bezierValueAnimator);//顺序播放
        finalSet.setInterpolator(line);//interpolators[random.nextInt(4)]
        finalSet.setTarget(target);
        return finalSet;
    }

    private AnimatorSet getEnterAnimtor(final View target) {

        ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f,
                1f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X,
                0.2f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y,
                0.2f, 1f);
        AnimatorSet enter = new AnimatorSet();
        enter.setDuration(500);
        enter.setInterpolator(new LinearInterpolator());
        enter.playTogether(alpha, scaleX, scaleY);
        enter.setTarget(target);
        return enter;
    }

    private ValueAnimator getBezierValueAnimator(View target) {

        // 初始化一个贝塞尔计算器- - 传入
        BezierEvaluator evaluator = new BezierEvaluator(getPointF(2),
                getPointF(1));

        // 这里最好画个图 理解一下 传入了起点 和 终点
        ValueAnimator animator = ValueAnimator.ofObject(evaluator, new PointF(
                        2 * (mWidth - dWidth) / 3, mHeight - dHeight),
                new PointF(random.nextInt(getWidth()), 0));
        animator.addUpdateListener(new BezierListenr(target));
        animator.setTarget(target);
        animator.setDuration(3000);
        return animator;
    }

    /**
     * 获取中间的两个 点
     *
     * @param scale
     */
    private PointF getPointF(int scale) {
        PointF pointF = new PointF();
        switch (scale) {
            case 1:
                pointF.x = random.nextInt((mWidth));// 减去100 是为了控制 x轴活动范围,看效果 随意~~
                // 再Y轴上 为了确保第二个点 在第一个点之上,我把Y分成了上下两半 这样动画效果好一些 也可以用其他方法
                pointF.y = random.nextInt((mHeight)) / 2 + mHeight / 2;
                break;
            case 2:
                pointF.x = random.nextInt((mWidth));// 减去100 是为了控制 x轴活动范围,看效果 随意~~
                // 再Y轴上 为了确保第二个点 在第一个点之上,我把Y分成了上下两半 这样动画效果好一些 也可以用其他方法
                pointF.y = random.nextInt((mHeight)) / scale;
                break;
        }
        return pointF;
    }

    private class BezierListenr implements ValueAnimator.AnimatorUpdateListener {

        private View target;

        public BezierListenr(View target) {
            this.target = target;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            // 这里获取到贝塞尔曲线计算出来的的x y值 赋值给view 这样就能让爱心随着曲线走啦
            PointF pointF = (PointF) animation.getAnimatedValue();
            target.setX(pointF.x);
            target.setY(pointF.y);
            // 这里顺便做一个alpha动画
            target.setAlpha(1 - animation.getAnimatedFraction());//数据的百分比
        }
    }

    private class AnimEndListener extends AnimatorListenerAdapter {
        private View target;

        public AnimEndListener(View target) {
            this.target = target;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            // 因为不停的add 导致子view数量只增不减,所以在view动画结束后remove掉
            removeView((target));
        }
    }
}

调用也简单,在布局(xml文件)里添加

 <com.eluton.view.bubble.PeriscopeLayout
        android:id="@+id/bubble"
        android:layout_width="100dp"
        android:layout_height="250dp"/>

然后bubble.addHeart()

相关文章

网友评论

      本文标题:点赞动画

      本文链接:https://www.haomeiwen.com/subject/unevuhtx.html