ObjectAnimator

本文深入讲解了Android中ObjectAnimator的基本用法及其实现原理。通过对比ValueAnimator,突出了ObjectAnimator的特点及其对动画目标对象属性的直接操作方式。并提供了一个具体的实现案例,帮助读者更好地理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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(非常厉害的一位博客大神)

  1. public static ObjectAnimator ofFloat(Object target, String propertyName, float... values)   设置setter(float valible)对应属性 target:是动画主体、propertyName是动画主体的属性名、values是一组可变长参数。
  2. 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();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值