视图动画只能作用于 View,而且视图动画改变的仅仅是 View的绘制效果,View的真正属性没有改变。而属性动画则可以改变真正的属性。大致的意思就是属性动画就是在一段时间内,按照一定的规律改变对象的属性,使对象展示出动画效果。摘要 : 记录一下 Animator中的 onStart(), cancel(),end(),pause(),setStartDelay(long startDelay),isPaused(),isStarted(),isRunning()的对应关系。
注意:属性动画的xml文件则放于res/animator/目录下。
Animator
可以看到 Animator是AnimatorSet、ValueAnimator、ObjectAnimator、TimeAnimator的父辈或者爷爷辈。并且Animator为一个抽象类。其定义属性有:
<declare-styleable name="Animator">
<!-- Defines the interpolator used to smooth the animation movement in time. -->
<attr name="interpolator" />
<!-- Amount of time (in milliseconds) for the animation to run. -->
<attr name="duration" />
<!-- Delay in milliseconds before the animation runs, once start time is reached. -->
<attr name="startOffset"/>
<!-- Defines how many times the animation should repeat. The default value is 0. -->
<attr name="repeatCount"/>
<!-- Defines the animation behavior when it reaches the end and the repeat count is
greater than 0 or infinite. The default value is restart. -->
<attr name="repeatMode"/>
<!-- Value the animation starts from. -->
<attr name="valueFrom" format="float|integer|color|dimension"/>
<!-- Value the animation animates to. -->
<attr name="valueTo" format="float|integer|color|dimension"/>
<!-- The type of valueFrom and valueTo. -->
<attr name="valueType">
<!-- valueFrom and valueTo are floats. This is the default value is valueType is
unspecified. Note that if either valueFrom or valueTo represent colors
(beginning with "#"), then this attribute is ignored and the color values are
interpreted as integers. -->
<enum name="floatType" value="0" />
<!-- valueFrom and valueTo are integers. -->
<enum name="intType" value="1" />
</attr>
</declare-styleable>
可能用到属性的大致意思 :
属性 | 含义 |
---|---|
android:interpolator | 动画速率变化的插值器 |
android:duration | 动画从开始到结束持续的时长,单位为毫秒 |
android:startOffset | 设置动画执行之前的等待时长,单位为毫秒 |
android:repeatCount | 设置动画重复执行的次数,默认为0,即不重复;可设为-1或infinite,表示无限重复 |
android:repeatMode | 设置动画重复执行的模式,可设为以下两个值其中之一:
|
android:valueFrom | 动画开始的值,可以为int值、float值或color值 |
android:valueTo | 动画结束的值,可以为int值、float值或color值 |
android:valueType | 参数值类型
|
方法名 | 大致描述 |
---|---|
public void start() {} | 开始这个动画,如果这个动画设置了 一个非0的startDelay()延迟,这个动画将在 延迟完成后在运行。 如果没有延迟则会立即执行。这个方法会 触发 AnimatorListener#onAnimationStart(Animator)方法。 动画运行在 调用这个方法的 线程上,这个线程必须要有Looper(否则就直接抛出异常)。如果一个View 要实现属性动画,应该在 UI线程。 |
public void cancel() { } | 取消这个动画,不同于 end()方法,它会停止在这个属性值的当前值。 它会触发AnimatorListener#onAnimationCancel(Animator)后,再触发AnimatorListener#onAnimationEnd(Animator)。这个方法必须运行在 调用动画运行的线程上 |
public void end() { } | 结束动画,跳到结束完成的状态,其属性值也会跟随为完成时的值。 这个方法必须运行在 调用动画运行的线程上 |
public void pause() {…} | 暂停这个动画,必须在 调用start() 方法的线程调用。如果这个动画没有start()或者执行完成(end()),将会直接忽略。 可以通过 resume()方法重新唤醒。会触发 AnimatorPauseListener#onAnimationPause |
public void resume() {…} | 恢复暂停的动画,从调用paused的地方开始,必须在 调用start() 方法的线程调用。如果没有调用 paused而调用此方法。它将会被负略。会触发 AnimatorPauseListener#onAnimationResume |
public boolean isPaused() {…} | 返回是否这动画目前处于暂停状态。 true表示是暂停状态,反之则是不是。 |
public abstract long getStartDelay(); | 获取去start()的延迟时间,单位为毫秒。在start()之后才可以调用。 |
public abstract void setStartDelay(long startDelay); | 设置有个 延迟的时间。单位毫秒,在 start()之后 |
public abstract Animator setDuration(long duration); | 设置动画执行的时常 单位毫秒 |
public abstract long getDuration(); | 获取动画执行的时长 单位毫秒 |
public abstract void setInterpolator(TimeInterpolator value); | 设置插值器, |
public TimeInterpolator getInterpolator() {…} | 获取我们设置的时间插值器 |
public abstract boolean isRunning(); | 返回 是否是运行状态,在start(),或设置的startDelay时间运行完,到 没有end(),之间 |
public boolean isStarted() {…} | 判断这个动画是否开始,由子类返回具体值 |
public void addListener(AnimatorListener listener) {…} | 包含了: void onAnimationStart(Animator animation); void onAnimationEnd(Animator animation); void onAnimationCancel(Animator animation); void onAnimationRepeat(Animator animation); |
public void addPauseListener(AnimatorPauseListener listener) {…} | 包含了: void onAnimationPause(Animator animation); void onAnimationResume(Animator animation); |
public Animator clone() {…} | Animator的clone()方法会对动画进行拷贝,子类可以重写该方法以实现深拷贝。 |
public void setTarget(Object target) { } | 可以通过调用动画的setTarget方法设置其要操作的对象,这样可以更新该对象的某个属性值。实际上,该方法对于ValueAnimator作用不大,因为ValueAnimator不是直接与某个对象打交道的。setTarget方法对于ObjectAnimator作用较大,因为ObjectAnimator需要绑定某个要操作的对象 |
public static interface AnimatorListener {
void onAnimationStart(Animator animation);
void onAnimationEnd(Animator animation);
void onAnimationCancel(Animator animation);
void onAnimationRepeat(Animator animation);
}
public static interface AnimatorPauseListener {
void onAnimationPause(Animator animation);
void onAnimationResume(Animator animation);
}
以下为没有设置setStartDelay(long 时 onStart(),cancel(),end(),pause(),startDelay),isPaused(),isStarted(),isRunning()的对应关系:
animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 800);
animator.setDuration(2000);
//
tv.setText(tv.getY()+"");
运行结果如下:
以上小节:在没有设置setStartDelay()的时候,当调用start()之后,isRuning和isStarted立刻改变为true。在蓝色TextView的值设置为 tv.getY(),在运行后其属性值也在跟随改变。当调用 cancel()的时候,其TextView的属性值,会停留在 当点击cancel()的值。当戳 end()时,直接表为运行完成状态。其属性值也为最终的值。
以下为设置setStartDelay(long 时 onStart(),把其值修改为900.
mAnimator = ObjectAnimator.ofFloat(tv, "translationY", 0, 900);
mAnimator.setDuration(2000);
为了细看一下,把其sleep的时间向后延时50毫秒
mAnimator.setStartDelay(2000);
//
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2050);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.sendEmptyMessage(0x110) ;
}
}.start();
以上小节:在设置setStartDelay()为2000毫秒的时候,当调用start()之后,isStarted立刻变为true,而 isRuning在Textview真正运行的时候才开始变为true。其它的没有变化。