贝塞尔曲线--属性动画

public class SplashLayout extends RelativeLayout {
    private Drawable[] drawables;
    private Random random;
    private int dWidth, dHeight;
    private int mWidth = 900, mHeight = 1500;
    private PointF poinF0;
    private PointF pointF1;
    private PointF pointF2;
    private PointF pointF3;
    //多声明几个差值器
    private AccelerateInterpolator acc = new AccelerateInterpolator();
    private AccelerateDecelerateInterpolator acd = new AccelerateDecelerateInterpolator();
    private DecelerateInterpolator add = new DecelerateInterpolator();
    private AnimatorSet set;


    public SplashLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    public SplashLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }
    public void initData() {
    //获取图片
        drawables = new Drawable[3];
        drawables[0] = getResources().getDrawable(R.drawable.ic_mineinfo);
        drawables[1] = getResources().getDrawable(R.drawable.ic_uncheck);
        drawables[2] = getResources().getDrawable(R.drawable.list_selected);
        //获取imageview 本身的宽高
        dWidth = drawables[0].getIntrinsicWidth();
        dHeight = drawables[0].getIntrinsicHeight();
        //生成随机数
        random = new Random();


        initView();
    }


    private void initView() {
    //动态添加imageView,设置宽高
        final ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[random.nextInt(3)]);
        LayoutParams params = new LayoutParams(dWidth, dHeight);
        addView(imageView, params);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);

        //获取属性动画集合
       set = getAnimatorSet(imageView);
        //监听动画,在动画播放完毕后删除imageView防止占用内存
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeView(imageView);

            }
        });
        //设置拦截器,加速,减速,加减速
        Interpolator []interpolators = new Interpolator[3];
        interpolators[0] = acc;
        interpolators[1] = acd;
        interpolators[2] = add;
        set.setInterpolator(interpolators[random.nextInt(3)]);
        set.start();
    }
        //获取动画集合
    private AnimatorSet getAnimatorSet(ImageView imageView) {

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.4f, 1f);
        scaleX.setRepeatCount(1000);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.4f, 1f);
        scaleY.setRepeatCount(1000);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.1f);
        alpha.setRepeatCount(1000);
        //贝塞尔曲线动画
        ValueAnimator baser = getBaserAnimator(imageView);
        baser.setRepeatCount(1000);


        //动画集合
        AnimatorSet set = new AnimatorSet();
        set.setDuration(3000);
        set.playTogether(scaleX, scaleY, alpha, baser);
        return set;
    }

    //获取贝塞尔曲线动画
    private ValueAnimator getBaserAnimator(final ImageView imageView) {

        //先需要四个点
        poinF0 = new PointF((mWidth - dWidth) / 2, (mHeight - dHeight));
        pointF3 = new PointF(random.nextInt(mWidth), 0);
        pointF1 = getPointF(1);
        pointF2 = getPointF(2);
        //需要一个baser估值器
        BezierEvaluator baserEvaluator = new BezierEvaluator(pointF1, pointF2);
        ValueAnimator baser = ValueAnimator.ofObject(baserEvaluator, poinF0, pointF3);

        baser.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
            }
        });


        return baser;
    }

    //贝塞尔曲线的中间两个点
    private PointF getPointF(int i) {
        PointF pointF = new PointF();
        pointF.x = random.nextInt(mWidth);
        if (i == 1) {
            pointF.y = random.nextInt(mHeight / 2) + mHeight / 2;
        } else {
            pointF.y = random.nextInt(mHeight / 2);
        }
        return pointF;
    }
    public void stop(){
        set.cancel();
        set.end();
    }

估值器

package com.anew.note.views;

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

/**
* Created by pig on 2017/3/3.
*/

public class BezierEvaluator implements TypeEvaluator {
private PointF pointF1;
private PointF pointF2;

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

@Override
public PointF evaluate(float t, PointF pointF0, PointF pointF3) {
    PointF pointF = new PointF();
    pointF.x = pointF0.x*(1-t)*(1-t)*(1-t)+
            3*pointF1.x*t*(1-t)*(1-t)+
            3*pointF2.x*t*t*(1-t)+
            pointF3.x*t*t*t;
    pointF.y = pointF0.y*(1-t)*(1-t)*(1-t)+
            3*pointF1.y*t*(1-t)*(1-t)+
            3*pointF2.x*t*t*(1-t)+
            pointF3.y*t*t*t;

    return pointF;
}

}

猜你喜欢

转载自blog.csdn.net/qq_22230935/article/details/60754876