android ObjectAnimator 详解

动画之-> ObjectAnimator

极客学院Animation教程讲解的很详细,点击进入哦

前言

先学习ValueAnimator,学习ObjectAnimator会容易很多~

一、秒懂ObjectAnimator

  • ObjectAnimator派生自ValueAnimator,所以ValueAnimator能用的方法,ObjectAnimator都能用, ObjectAnimator 是ValueAnimator的子类。
  • ObjectAnimator重载了几个方法,例如ofInt(),ofFloat()等,这里我们以ofFloat()做个例子
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0,1);  
animator.setDuration(2000);  
animator.start(); 
  • 可以看出,较之以前多了两个参数,那这两个参数表示什么意思呢?且听我一一道来
    • 第一个参数用于指定这个动画要操作的是哪个控件
    • 第二个参数用于指定这个动画要操作这个控件的哪个属性
    • 第三个参数,和ValueAnimator是一样的
      有木有方便很多 ��

二、ObjectAnimator的函数

  1. 改变旋转度数 rotation、rotationX、rotationY
  • float rotationX:表示围绕 X 轴旋转,rotationX 表示旋转度数
  • float rotationY:表示围绕 Y 轴旋转,rotationY 表示旋转度数
  • float rotation:表示围绕 Z 旋转,rotation 表示旋转度数
    举个栗子:
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"rotationX",0,270,50);  
animator.setDuration(2000);  
animator.start();


从这张图中,绿色框部分表示手机屏幕,很明显可以看出 Z 轴就是从屏幕左上角原点向外伸出的一条轴。

2.移动 translationX、translationY

  • float translationX :表示在 X 轴上的平移距离,以当前控件为原点,向右为正方向,参数 translationX 表示移动的距离。
  • float translationY :表示在 Y 轴上的平移距离,以当前控件为原点,向下为正方向,参数 translationY 表示移动的距离。
    举个栗子:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200,0);  
animator.setDuration(2000);  
animator.start();  

指定的移动距离是(0, 200, -200,0),所以控件会从自身所有位置向右移动 200 像素,然后再移动到距离原点-200 的位置,最后回到原点

3.缩放 scaleX、scaleY

  • float scaleX:在 X 轴上缩放,scaleX 表示缩放倍数
  • float scaleY:在 Y 轴上缩放,scaleY 表示缩放倍数
    用法和上边的一致,不再一一举例

4.改变透明度 alpha

  • float alpha:改变透明度

5.如何实现这些改变的呢?看图

引用自http://wiki.jikexueyuan.com/project/android-animation/7.html

例如:

ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0,1);  
animator.setDuration(2000);  
animator.start(); 

在ObjectAnimator中会根据alpha拼装成setAlpha函数,然后通过反射找到view控件对应个setAlpha方法,将当前的数值传入进去。

三、自定义ObjectAnimator的属性

1. 注意事项

  1. 拼接set函数:在拼接函数时,强制将属性的第一个字母大写,其他不变。例如alpha->setAlpha,不过,setAlpha->alpha/Alpha都可以。
  2. 确定函数的参数类型:使用ofFloat建立的,set函数中的参数也应该是float类型。

2. 举个栗子

引用自http://wiki.jikexueyuan.com/project/android-animation/6.html


还是这个gif,用ObjectAnimator实现O(∩_∩)O~

  1. 建立实体类
/** 
可以添加多种圆的属性,可以设置半径、颜色等,这里仅设置半径 
*/
public class Circle {
    //圆的半径
    private int radius;
    public Circle(int radius) {
        this.radius = radius;
    }
    public int getRadius() {
        return radius;    
} 
   public void setRadius(int radius) {
        this.radius = radius;
    }
}

2.自定义View,绘制圆形

public class MyPointView extends View {
    private Circle mCircle = new Circle(0);
    public MyPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        if (mCircle != null){
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawCircle(400,500,mCircle.getRadius(),paint);
        }
        super.onDraw(canvas);
    }
//get函数
    public int getCircleRadius(){
        return 800;
    }
//set函数
    public void setCircleRadius(int radius){ 
       mCircle.setRadius(radius);
        invalidate();
    }
} 

3.xml布局中引用

<com.demo.animator.MyPointView
    android:id="@+id/view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

4.Activity中调用一下,OVER!

//正常调用,初始值40
ObjectAnimator animator = ObjectAnimator.ofInt(view, "circleRadius",40,400);
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(2000);
animator.start();

注意:
//当且仅当动画的只有一个过渡值时,系统才会调用对应属性的 get 函数来得到动画的初始值。

//这样子,初始值是800
// ObjectAnimator animator = ObjectAnimator.ofInt(view, "circleRadius",400);

后记
这个动画,还可以用ofObject实现哦,
例如:

ObjectAnimator animator = ObjectAnimator.ofObject(view, "circle", new CircleEvaluator(), new Circle(400));

只需要在自定义view中设置相应的set/get方法就好啦~

转自:https://www.jianshu.com/p/166e6180865b

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值