ValueAnimator.ofObject的使用

ValueAnimator.ofObject的使用

1.activity_anim.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:text="开始"/>

    <www.weshared.anim.PointView  android:id="@+id/ponitview" android:layout_width="200dp" android:layout_height="200dp" android:background="@android:color/transparent" android:layout_centerInParent="true" />

</RelativeLayout>

2.自定义PointView控件

public class PointView extends TextView {

    private Paint mPaint;
    private int mColor = Color.TRANSPARENT;
    private Point point;
    private boolean isFirst;

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

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

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

    /** * 初始化画笔,设置颜色为蓝色 */
    private void init() {
        mColor = Color.CYAN;
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        mPaint.setColor(mColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height = getHeight();
        int width = getWidth();
        int initR = Math.min(width, height) / 2;
        if (!isFirst) {
            //初始化点
            point = new Point(initR);
            isFirst = true;
        }
        //画圆
        canvas.drawCircle(width / 2, height / 2, point.getR(), mPaint);
    }

    public void showCharAnim() {
        ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('z'));
        animator.setDuration(5000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                char animatedValue = (char) animation.getAnimatedValue();
                //将文字展示到控件上
                PointView.this.setText(String.valueOf(animatedValue));
                PointView.this.setTextSize(30);
                PointView.this.setTextColor(Color.RED);
                invalidate();
            }
        });
    }

    public void showColorAnim(){
        ValueAnimator animator = ValueAnimator.ofArgb(0xFFFF0000, 0xFF0000FF, 0xFFFF0000, 0xFF0000FF);
        animator.setDuration(5000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                PointView.this.setTextColor(animatedValue);
                invalidate();
            }
        });
    }

    public void showPointAnim() {
        float width = getMeasuredWidth();
        float height = getMeasuredHeight();
        float r = Math.min(width, height) / 2;

        //用ObjectAnimator也可以实现,不过和属性值无关了,ObjectAnimator是ValueAnimator的子类,实际上还是通过addUpdateListener执行了ValueAnimator动画
// ObjectAnimator animator = ObjectAnimator.ofObject(this, "---", new PointEvaluator(), new Point(r * 0.1f), new Point(r * 1.2f), new Point(r * 0.3f), new Point(r));
        ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(), new Point(r), new Point(r * 0.2f), new Point(r * 1.2f), new Point(r * 0.2f), new Point(r));
        animator.setInterpolator(new OvershootInterpolator());
        animator.setDuration(5000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                point = (Point) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    public class PointEvaluator implements TypeEvaluator<Point> {

        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            //拿到point的半径值
            float startValueR = startValue.getR();
            float endValueR = endValue.getR();
            float currentValueR = startValueR + (endValueR - startValueR) * fraction;
            return new Point(currentValueR);
        }
    }

    public class CharEvaluator implements TypeEvaluator<Character> {

        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {

            int start = startValue;
            int end = endValue;
            char current = (char) (start + (end - start) * fraction);
            return current;
        }
    }
}

在MainActivity文件中

public class AnimActivity extends AppCompatActivity {

    private Button mButton;
    private PointView mPonitView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
    }

    private void initView() {
        setContentView(R.layout.activity_anim);
        mButton = (Button) findViewById(R.id.btn);
        mPonitView = (PointView) findViewById(R.id.ponitview);
    }

    private void initListener() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPonitView.showPointAnim();
                mPonitView.showCharAnim();
                mPonitView.showColorAnim();
            }
        });
    }
}

Point类

public class Point {

    public float r;
    public float x;
    public float y;

    public Point(float r){
        this.r=r;
    }

    public Point(float x, float y,float r){
        this.x=x;
        this.y=y;
        this.r=r;
    }

    public void setR(float r) {
        this.r = r;
    }

    public void setX(float x) {
        this.x = x;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getR() {
        return r;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }
}

你可能感兴趣的:(android,属性动画,ofObject,值动画)