转载请标明出处: http://blog.youkuaiyun.com/airsaid/article/details/52074566
本文出自:周游的博客
前言
前面博文中写了逐帧 和补间动画 的使用,今天有时间来继续写写属性动画。
简介
众所周知,属性动画是Android3.0版本开始的,一个东西的推出肯定是有它的道理的,那为什么前面已经有逐帧和补间动画了还要推出属性动画呢?
在前2篇的补间动画中我们知道了我们可以对一个View进行:缩放、旋转、平移、淡入淡出操作,并且可以做自定义补间动画的操作。但是自定义补间动画操作起来很麻烦,而自带的那几种使用起来虽然简单,但是也仅限于这几种了,补间动画的机制其实就是使用硬编码来完成的,功能已经限定死了,基本没有任何扩展性可言。还有就是如果我们使用补间动画使View往右移动一段距离,那么其实这个View依然还在原点,并没有真正移动,而使用属性动画移动到那个点就是在那个点。还有一个重要的点是,补间动画只能够作用在View上的,那么如果你想对其他非View对象进行动画操作,那就GG了。说了这么多,相信大家已经知道补间和属性动画的区别了。
属性动画其对应的类是Animator,和补间动画中的Animation一样,Animator也是一个抽象类,其对应的继承关系如下图:
Animator
Animator有如下方法:
* addListener(Animator.AnimatorListener listener):监听动画的开始、重复、和结束。
* addPauseListener(Animator.AnimatorPauseListener listener):为动画添加一个暂停监听。
* cancel() :取消动画
* clone():对动画进行拷贝。
* end():结束动画。
* getDuration():获取动画的持续时间。
* getInterpolator():获取动画的时间插补器。
* getListeners():获取监听器集合。
* getStartDelay():获取动画延迟开始的时间。
* isPaused():检查动画是否处于暂停状态。
* isRunning():检查动画是否正在运行(已经启动,并走过了startDelay(),尚未结束)
* isStarted():检查动画是否已经开始,没有结束。
* pause():暂停正在运动的动画。
* removeAllListeners():删除所有的listeners 与pauseListeners对象。
* removeListener(Animator.AnimatorListener listener):移除指定的AnimatorListener监听器。
* removePauseListener(Animator.AnimatorPauseListener listener):移除指定的AnimatorPauseListene监听器。
* resume():恢复已经暂停的动画。
* setDuration(long duration):设置动画持续时间。
* setInterpolator(TimeInterpolator value):设置时间插值器。
* setStartDelay(long startDelay):设置动画延迟多少毫秒后start()方法被调用。
* setTarget(Object target):设置目标对象。
* start():启动动画。
由于Animator是属性动画的基类,那么其子类也都有上面的方法。我们在具体使用中我们通常会使用它的子类,下面具体来看看子类的使用。
ObjectAnimator
首先来用ObjectAnimator实现下补间动画中的操作。因为ObjectAnimator继承自ValueAnimator,使用起来是最简单的,分分钟实现一个动画:
* 新建一个布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.airsaid.propertyanimationdemo.MainActivity">
<ImageView
android:onClick="startAnim"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
- 代码中实现点击事件开启了一个旋转动画:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startAnim(View v){
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(v, "rotation", 0.0f, 360f);
rotateAnim.setDuration(3000);
rotateAnim.start();
}
}
运行结果:
可以看到,我们调用ofFloat(Object target, String propertyName, float… values) 构建ObjectAnimator时候,只传入了这个动画要操作的View,以及操作这个View的属性,还有就是可变参,就实现了一个动画。
那么问题来了,除了传入rotation属性之外,还可以传入啥属性呢?
其实在我们使用ObjectAnimator做动画的时候,并不是根据XML中定义的属性来改变的,而是去通过指定的属性去查找所对于的get、set方法来改变的。那我们上面的操作的ImageView有setRotation()方法么?答案是有的。都是通过View继承而来的。我们可以看看View中一共有哪些相关的方法:
//1、透明度:alpha
public void setAlpha(float alpha)
//2、旋转度数:rotation、rotationX、rotationY
public void setRotation(float rotation)
public void setRotationX(float rotationX)
public void setRotationY(float rotationY)
//3、平移:translationX、translationY
public void setTranslationX(float translationX)