Android 插值属性动画Property Animation
来源:互联网 发布:ae cc mac下载 编辑:程序博客网 时间:2024/06/11 19:39
二、属性动画系统可以定义以下动画特性:
1. 持续时间(Duration):指定动画的持续时间。默认长度是300毫秒。
2. 时间插值(Time interpolation):这个值能够做为计算当前动画运行时间的函数的属性值来指定,它决定动画的变化频率。
3. 重复次数和行为(Repeat count and behavior)
这个属性能够指定在动画结束时是否重新播放动画,以及重复播放的次数。还能够指定动画是否能够反向回播,如果设置了反向回播,那么动画就会先向前再向后,重复播放,直到达到播放次数。
4. 动画集合(Animator sets):你能够把动画组织到一个逻辑集合中,然后或者同时、或者顺序的、或者延迟播放它们。
5. 帧刷新延迟(Frame refresh delay):你能够指定动画帧的刷新频率。默认是每10秒中刷新一次,但是应用程序最终的刷新帧的速度依赖与系统的繁忙程度以及系统能够提供的底层定时器的反应速度
三、使用
public final class ObjectAnimator extends ValueAnimatorpublic static ObjectAnimator ofFloat(Object target, String propertyName, float... values)1、使用实例:
ObjectAnimator animator = ObjectAnimator.ofFloat(myTextView, "alpha", 0.0f, 1.0f, 0.0f); animator.setDuration(3000); animator.start();
同理propertyName可以设为rotation,translationX,translationY,scaleX,scaleY来实现相应功能;
2、xml资源文件形式使用:
在res/animator文件夹下建立相应xml文件:
<?xml version="1.0"encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:valueFrom="1.0" android:valueTo="0.0" android:valueType="floatType" android:propertyName="alpha" android:duration=" 1000" android:repeatCount="-1" android:repeatMode="reverse"></objectAnimator>在代码中使用如下:
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.myanimator); animator.setTarget(myTextView); animator.start();3、监听器AnimatorListener
animator.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationCancel(Animator animation) { } });如果不想实现这么多方法,仅想实现其中的一个或多个方法,可以使用AnimatorListenerAdapter
animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { } });看一下AnimatorListenerAdapter的源码:(pause,reusme是AnimatorPauseListener中的方法)
public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener, Animator.AnimatorPauseListener { @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationPause(Animator animation) { } @Override public void onAnimationResume(Animator animation) { }}
四、组合动画AnimatorSet
1、实现组合动画功能主要需要借助AnimatorSet这个类,这个类提供了一个play()方法,如果我们向这个方法中传入一个Animator对象(ValueAnimator或ObjectAnimator)将会返回一个AnimatorSet.Builder的实例,AnimatorSet.Builder中包括以下四个方法:
- after(Animator anim) 将现有动画插入到传入的动画之后执行
- after(long delay) 将现有动画延迟指定毫秒后执行
- before(Animator anim) 将现有动画插入到传入的动画之前执行
- with(Animator anim) 将现有动画和传入的动画同时执行
2、示例:
ObjectAnimator firstAnimator = ObjectAnimator.ofFloat(myTextView, "translationX", -500f, 0f); ObjectAnimator secondAnimator = ObjectAnimator.ofFloat(myTextView, "rotation", 0f, 360f); ObjectAnimator thirdAnimator = ObjectAnimator.ofFloat(myTextView, "alpha", 1f, 0f, 1f); AnimatorSet animSet = new AnimatorSet(); animSet.play(firstAnimator).with(secondAnimator).after(thirdAnimator); animSet.setDuration(5000); animSet.start();
3、XML资源文件形式使用得:(类似于上面的使用方法)
在res/animator文件夹下创建xml文件:
<?xml version="1.0"encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator android:duration="2000" android:propertyName="translationX" android:valueFrom="-500" android:valueTo="0" android:valueType="floatType"> </objectAnimator> <set android:ordering="together"> <objectAnimator android:duration="3000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="360" android:valueType="floatType"> </objectAnimator> <set android:ordering="sequentially"> <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:valueType="floatType"> </objectAnimator> <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:valueType="floatType"> </objectAnimator> </set> </set> <!-- ordering表示组合的播放顺序:sequentially表示顺序执行,together表示同时执行 --> </set>在代码中使用:
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.myset); animator.setTarget(myTextView); animator.start();
4、使用propertyValuesHolder来使得一个动画实现多种效果:
// 组合形式使用动画PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f, 1.3f);PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f, 1.3f);ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(loadingImg, pvhX, pvhY);animator.setDuration(DURATION_TIME);
- Android 插值属性动画Property Animation
- Android Property Animation 属性动画
- Android Property Animation 属性动画
- Android属性动画---Property Animation
- Android-Property Animation(属性动画)
- Android 属性动画(Property Animation)
- Android 属性动画(Property Animation)
- Android Property Animation属性动画
- Android属性动画(Property Animation)
- Android属性动画(Property Animation)
- Android动画三:属性动画 (property animation)
- Android动画--属性动画Property Animation
- Android属性动画Property Animation,组合动画
- Android属性动画---Property Animation(一)
- Android属性动画---Property Animation(二)
- Android属性动画---Property Animation(三)
- Android属性动画---Property Animation(四)
- Android属性动画---Property Animation(五)
- 源码浅析: Message/Handler/MessageQueue/Looper
- 数据存储Introduce
- java自动类型提升
- android——代码实现在指定位置显示View
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
- Android 插值属性动画Property Animation
- 在Linux上配置同时支持ASP.NET和PHP的服务器
- 基于用户投票的排名算法(五):威尔逊区间
- OCR:几大开源库
- 禁止KKV.exe开机自启动办法
- Activity之间使用动画跳转
- PerparedStatement的使用,及模糊查询时候的参数注入
- 基于用户投票的排名算法(六):贝叶斯平均
- Try...catch...finally与直接throw的区别