ObjectAnimator动画的基本使用
之前学习了ValueAnimator的相关知识,可以看出,ValueAnimator是对值进行计算,直接操作的是值,即使ofObject()也是对自定义的对象进行操作,然后在监听中不断回调拿到当前的值或者自定义对象,再次操作动画实施的对象。
而ObjectAnimator则是直接操作动画实施对象,在动画实施对象中需要有设置属性的setter()和getter(),其中setter是用来在动画插值器产生值后设置,并且需要在setter中做动画的后续操作。如果没有getter,那么如果只给了一个值那么会有警告offint的初始值为0,offfloat初始值为0.0。
ObjectAnimator的流程,引用自启舰的csdn 可参考这里http://blog.youkuaiyun.com/harvic880925/article/details/50995268(非常厉害的一位博客大神)
- public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) 设置setter(float valible)对应属性 target:是动画主体、propertyName是动画主体的属性名、values是一组可变长参数。
- public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)
public static ObjectAnimator ofInt(Object target, String propertyName, int... values)
public static ObjectAnimator ofObject(Object target, String propertyName,TypeEvaluator evaluator, Object... values)
package com.stu.grd.animatordemo.objectanimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.stu.grd.animatordemo.Point;
/**
* Created by guoruidong on 2017/12/7.
*/
public class MyPointView extends View {
private Paint mPaint;
private Point mPoint = new Point(100);
public MyPointView(Context context) {
this(context,null);
}
public MyPointView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyPointView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mPaint != null) {
canvas.drawCircle(600,600,mPoint.getRadious(),mPaint);
}
}
public int getPoint() {
return mPoint.getRadious();
}
public void setPoint(Point point){
this.mPoint = point;
invalidate();
}
public void setPoint(int radius) {
if (mPoint==null){
mPoint = new Point(radius);
}else {
mPoint.setRadious(radius);
}
invalidate();
}
}
private void doObjectAnimator(){
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mMyPointView,"point",300);
objectAnimator.setDuration(3000);
objectAnimator.start();
}