效果图:

 

Property Animation介绍:

出生在sdk3.0,是利用了View所拥有的属性,进行一系列的操作。

比方一个View有什么样的setAbc的属性,那么理论上就能够设置它。

它不仅改变View的绘制,也改变了View的属性;而Tween Animation 仅仅改变View的绘制。

Animator为属性动画的基类

其结构:

Animator abstract class

---- AnimatorSet final class 属性动画的集合,能够加入以下的值动画和对象对象在其内。可同一时候执行或顺序执行

----ValueAnimator 值动画。监听某一值的变化,进行对应的操作

---- ObjectAnimator final class 对象动画

ValueAnimator 值动画,它有一个子类ObjectAnimator。

须要Interpolator和TypeEvaluator来计算属性值。

TimeInterpolator 时间插入器 接口,反应动画的运动速率。

getInterpolation()依据一定算法,返回时间比率

----Interpolator interface 继承自TimeInterpolator

Interpolator 接口的实现类 见下表:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamp3d21scDQ1Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="608" height="351" alt="">

xml 设置 插入器:android:interpolator="@android:anim/accelerate_decelerate_interpolator"

TypeEvaluator<T> interface 计算类型值 支持泛型

方法:T evaluate(float fraction, T startValue, T endValue)

就是用来计算属性值的,就可以计算随意类型的值。

fraction 表示时间的比率。

实现类有: ArgbEvaluator ARGB颜色计算器

FloatEvaluator float型计算器

IntEvaluator int计算器

PropertyValuesHolder 属性值持有者  持有属性名、開始与结束值。

属性设置方法:setIntValues()、setFloatValues()、setObjectValues()、setKeyframes

它的静态的一些of方法,创建 PropertyValuesHolder 对象。 ofInt、ofFloat、ofObject、ofKeyframe

setEvaluator(eval); //设置计算器。

Keyframe  表示 a time/frame-value pair. 即含有 时间比率和帧值 属性

它的静态的一些of方法,创建
Keyframe 对象。  ofInt、ofFloat、ofObject

KeyframeSet 就是一组KeyFrame

它的静态的一些of方法。创建 KeyframeSet 对象。
 ofInt、ofFloat、ofObject、ofKeyframe

属性动画的一些说明:
1. ObjectAnimator 对象动画,当一个view同一时候拥有某一属性的getter、setter方法时,则能够使用该动画,来操作这一属性。
2. ValueAnimator 操作的范围比較广。通过Interpolator和TypeEvaluator得到某一时间内的值;再用监听器,监听值的变化,做对应的操作。
3. ValueAnimator 和ObjectAnimator(它是前者的子类)的静态的一些of方法。创建自身对象。
也能够new 一个无參的对象。再设置对应的values。
ofInt()、ofFloat()、ofObject()、ofPropertyValuesHolder()。
这几个方法的实现也就是new 一个无參的对象。再设置对应的values。 4. ValueAnimator 的调用流程:
a. 初始化ValueAnimator后,设置一个values。这时就有了一个PropertyViewHolder对象pvh。
能够直接调用setValues设置它PVH对象;或setInt|Float|ObjectValues
方法内部会生成一个PVH
PVH内部维护一个KeyframeSet和TypeEvaluator。PVH依据不同的values来初始化KeyframeSet和
TypeEvaluator实现方法中的startValue和endValue就从KeyframeSet中的Keyframe中获取
b. 设置TypeEvaluator。传递到pvh中。
c. 设置Interpolator。 ValueAnimator中默认的插入器为AccelerateDecelerateInterpolator
d. ValueAnimator的animationFrame(long currentTime),当有动画应该结束时返回true,否则返回false。 方法内,算出动画执行的时间比率fraction,再调用animateValue(float fraction)。 e. ValueAnimator的animateValue(float fraction)。调用插入器,得到一个按某一规则得到的fraction,
再调用 pvh.calculateValue(fraction);pvh调用KeyframeSet的getValue(fraction)。
KeyframeSet内部再调用TypeEvaluator的evaluate(fraction,T startValue, T endValue)。
startValue、endValue是通过Keyframe的getValue()来获取的。
f. evaluate(),方法内拿到了时间比率fraction,能够自行依据一定规则。返回value T。
g. 给ValueAnimator加入一个AnimatorUpdateListener。监听的回调方法:
onAnimationUpdate(ValueAnimator animation) {
T obj = (T)animation.getAnimatedValue();//取得计算器计算出的某段时间内的T值。 // 操作 obj
}

先来一个使用自己定义类型的求值计算器的ValueAnimator的样例

	ValueAnimator backAnim;
ImageView view; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.aa); view = (ImageView) findViewById(R.id.iv_img); backAnim = new ValueAnimator();
backAnim.setTarget(view);
backAnim.setDuration(2000); //类型求值<Drawable>:求出某一时间比值内的Drawable值
TypeEvaluator<Drawable> drawableEvaluator = new TypeEvaluator<Drawable>() { @Override //fraction 当前执行的时间比上总持续时间的 比值(中间经过插入器的规则运算)
public Drawable evaluate(float fraction, Drawable startValue,
Drawable endValue) {
System.out.println(fraction);
if (fraction < 0.5) {
return startValue;
} else {
return endValue;
}
}
}; //能够直接设置PVH对象。也能够先设置values,再设置TypeEvaluator
// PropertyValuesHolder pvh = PropertyValuesHolder.ofObject("imageDrawable", drawableEvaluator,
// getResources().getDrawable(R.drawable.ic_launcher), getResources().getDrawable(R.drawable.a1));
// backAnim.setValues(pvh);
backAnim.setObjectValues(getResources().getDrawable(R.drawable.ic_launcher), getResources().getDrawable(R.drawable.a2));
backAnim.setEvaluator(drawableEvaluator); backAnim.addUpdateListener(new AnimatorUpdateListener() { @Override
public void onAnimationUpdate(ValueAnimator animation) {
Drawable value = (Drawable) animation.getAnimatedValue();
view.setImageDrawable(value);
}
});
backAnim.setInterpolator(new CycleInterpolator(2));
backAnim.start();
}

3.0以后新增了一些View的属性:

1)translationX 和 translationY:这两个属性控制了View所处的位置,

     它们的值是由layout容器设置的,是相对于坐标原点(0,0左上角)的一个偏移量。

2)rotation, rotationX 和 rotationY:控制View绕着轴点(pivotX和pivotY)旋转。它的表现跟Tween Animation中的RotateAnimation不一致。

RotateAnimation 的旋转,表现为平面的旋转

而rotationX、Y 旋转。是立体的旋转,默认是以View的中心点。做rotation(x,y)过中心点的直线。面向该直线进行翻转

3)scaleX 和 scaleY:控制View基于pivotX和pivotY的缩放。

4)pivotX 和 pivotY:旋转的轴点和缩放的基准点,默认是View的中心点。

5)x 和 y:描写叙述了view在其父容器中的终于位置,是左上角坐标和偏移量(translationX。translationY)的和。

6)aplha:透明度,1是全然不透明,0是全然透明。

以上这些属性与Tween Animation的动画属性值差点儿相同

ObjectAnimator 对象动画

该动画。一次仅仅能表示一个动作属性。

ObjectAnimator的xml实现

xml定义动画

res/animator/scale_object_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="scaleX"
android:repeatCount="1"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" > </objectAnimator>

代码载入 动画xml

imageview_scale.setBackground(getResources().getDrawable(R.drawable.a11));
ObjectAnimator scaleAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.scale_object_animator);
scaleAnimator.setTarget(imageview_scale);//设置动画作用的目标对象
scaleAnimator.setDuration(1000);
scaleAnimator.setRepeatCount(50);
scaleAnimator.start();

AnimatorSet 动画集

由ObjectAnimator 和 ValueAnimator 组成,相应的xml中的写法 类似为 <set> <objectAnimator /> ... <animator />... </set>

xml定义动画集

res/animator/set_rotate_scale.xml

<?xml version="1.0" encoding="utf-8"?

>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together"> <!-- android:ordering together表示同一时候执行动画, sequentially 表示按顺序执行下面动画 -->
<set>
<objectAnimator
android:propertyName="rotationX"
android:repeatCount="50"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="20" />
<objectAnimator
android:propertyName="rotationY"
android:repeatCount="50"
android:repeatMode="reverse"
android:valueFrom="0"
android:valueTo="45"
android:valueType="floatType" />
</set>
<set>
<objectAnimator
android:propertyName="scaleX"
android:repeatCount="50"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>
<objectAnimator
android:propertyName="scaleY"
android:repeatCount="50"
android:repeatMode="reverse"
android:valueFrom="1.0"
android:valueTo="2.0" >
</objectAnimator>
</set> </set>

代码载入 动画集的xml

imageview_rotate.setBackground(getResources().getDrawable(R.drawable.a11));
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_rotate_scale);
animatorSet.setTarget(imageview_rotate);
animatorSet.setDuration(1000);
animatorSet.setInterpolator(new BounceInterpolator());//设置end时的弹跳插入器
animatorSet.start();

PropertyValuesHolder

//使用PropertyValuesHolder 构造 Animator   组合成相似set的效果
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("scaleX",0f,2.5f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleY",0f,3f);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageview, pvhX,pvhY);
animator.setDuration(2000);
animator.start();

APIDemo中的MultiPropertyAnimation演示样例:

  // ============================================================
// 第四个小球:利用关键帧实现曲线运动
ball = balls.get(3);
// 属性1:Y坐标运动:下落
pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(),
getHeight() - BALL_SIZE);
float ballX = ball.getX();
// 三个关键帧
Keyframe kf0 = Keyframe.ofFloat(0f, ballX); //參数为 time/value, 即时间点和指定值。还有这些构造ofInt()、ofObject()
Keyframe kf1 = Keyframe.ofFloat(.5f, ballX + 100f);
Keyframe kf2 = Keyframe.ofFloat(1f, ballX + 50f);
// 属性2:X坐标运动:曲折
// 用三个关键帧构造PropertyValuesHolder对象
PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe(
"x", kf0, kf1, kf2); // 再用两个PropertyValuesHolder对象构造一个ObjectAnimator对象。 <span style="color:#ff6666;">一个holder持有一个属性</span>
ObjectAnimator yxBouncer = ObjectAnimator
.ofPropertyValuesHolder(ball, pvhY, pvhX).setDuration(
DURATION / 2);
yxBouncer.setRepeatCount(1);
yxBouncer.setRepeatMode(ValueAnimator.REVERSE);

ViewPropertyAnimator 多属性动画

通过view.animate()来获取ViewPropertyAnimator。该类。能直接操作多个属性的动画。

imageview.setBackground(getResources().getDrawable(R.drawable.a11));
ViewPropertyAnimator animate = imageview.animate();//该对象没有setRepeat的方法
//通过一些动画属性来设置 组合成相似set的效果
animate.alpha(0);
animate.rotationX(50);
animate.translationXBy(500);
animate.scaleX(1.5f);
animate.scaleY(1.5f);
animate.setInterpolator(new BounceInterpolator());
animate.setDuration(2000);
animate.start();

ValueAnimator 值动画

ValueAnimator代码和xml设置中 没有setPropertyName 由于不是操作对象,仅仅是依据value进行某种动作

须要加监听器,监听值的变化 做对应的处理

xml定义值动画

<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="10000"
android:startOffset="1000"
android:repeatCount="infinite"
android:repeatMode="restart"
android:valueFrom="1"
android:valueTo="100"
android:valueType="intType"> </animator>

代码载入 值动画xml

ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator);
valueAnimator.setTarget(tv_num);
valueAnimator.setEvaluator(new TypeEvaluator<Integer>() { @Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
System.out.println("时间比率。fraction:" + fraction);
System.out.println("结果值:" + (int)((startValue + fraction * (endValue - startValue)) / 10 * 10));
return (int)((startValue + fraction * (endValue - startValue)) / 10 * 10);
}
});
valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override
public void onAnimationUpdate(ValueAnimator animation) {
//在onAnimationUpdate中 该值返回第一个动画的 当前帧的evaluate 值
System.out.println("animation.getAnimatedValue()==" + animation.getAnimatedValue());
tv_num.setText(animation.getAnimatedValue() + "");
}
});
// valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.start();

Animator的监听器

AnimatorListener

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 监听器的默认空实现

new AnimatorListenerAdapter() {//空实现了AnimatorListener。有下面6个方法(4个必须实现的方法和2个重写的方法)

			@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
} @Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
} @Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
} @Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
} @Override
public void onAnimationPause(Animator animation) {
super.onAnimationPause(animation);
} @Override
public void onAnimationResume(Animator animation) {
super.onAnimationResume(animation);
} }

AnimatorUpdateListener 动画的值更新监听器

new AnimatorUpdateListener() {

			@Override
public void onAnimationUpdate(ValueAnimator animation) {
//在onAnimationUpdate中 该值返回第一个动画的 当前帧的evaluate 值
System.out.println("animation.getAnimatedValue()==" + animation.getAnimatedValue());
}
}

一些操作函数:

animator.pause();  animator.resume(); animator.reverse(); animator.end(); animator.cancel();

animator.start(); animator.isStarted(); animator.isPaused(); animator.isRunning();

用属性动画换背景色

详见ApiDemo要下的 BouncingBalls.java

private static final int RED = 0xffFF8080;
private static final int BLUE = 0xff8080FF;
private static final int CYAN = 0xff80ffff;
private static final int GREEN = 0xff80ff80; {
//动画 变色
ObjectAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", CYAN, BLUE, RED);
colorAnim.setTarget(ll_animation);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.setDuration(3000);
colorAnim.start();
}

LayoutTransition

/*
* ViewGroup中使用LayoutTransition 进行 监听布局的改变。而创建动画
* LayoutTransition.APPEARING 新增出现时
* CHANGE_APPEARING
* CHANGE_DISAPPEARING 子view消失时
* CHANGING
* ViewGroup布局中:android:animateLayoutChanges="true" 使用的默认的LayoutTransition制作动画
*/
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(5000);
layoutTransition.setAnimator(LayoutTransition.APPEARING, scaleAnimator);
ll_animation.setLayoutTransition(layoutTransition); final TextView tv = new TextView(this);
tv.setWidth(100);
tv.setHeight(100);
tv.setText("中华人民共和国");
ll_animation.addView(tv);//相应type = APPEARING ll_animation.postDelayed(new Runnable() { @Override
public void run() {
ll_animation.removeView(tv);
}
}, 2000);

參考样例见:ApiDemos下的  LayoutAnimations.java

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Android Property Animation 物业动画的更多相关文章

  1. Android Property Animation动画

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  2. Android -- Property Animation

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  3. Android 动画——属性动画Property Animation

    Android在3.0之前只提供了两种动画:View Animation .Drawable Animation .也就是我们在<Android 动画——Frame Animation与Twee ...

  4. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  5. [Android Pro] Property Animation

    声明:下面的内容需要Android API level 11的支持 Property Animation是如何运作的 首先,来看一下两个不一样的Property Animation场景: 场景一(Li ...

  6. Android基础夯实--重温动画(一)之Tween Animation

    心灵鸡汤:真正成功的人生,不在于成就的大小,而在于你是否努力地去实现自我,喊出自己的声音,走出属于自己的道路. 摘要 不积跬步,无以至千里:不积小流,无以成江海.学习任何东西我们都离不开扎实的基础知识 ...

  7. Android开发之三种动画

    转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...

  8. Android:Animation

    Android 之 Animation 关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式:1. Tween Animation:通过对场 ...

  9. Android物业动画研究(Property Animation)彻底解决具体解释

     前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...

随机推荐

  1. JAVA逆向&反混淆-追查Burpsuite的破解原理(转)

    0x00 摘要: 本系列文章通过对BurpLoader的几个版本的逆向分析,分析Burpsuite的破解原理,分析Burpsuite认证体系存在的安全漏洞. 0x01 JD-GUI的用途与缺陷: JD ...

  2. Cordova CLI源码分析(二)——package.json

    每个包需要在其顶层目录下包含一个package.json文件,该文件不仅是包的说明,也影响npm安装包时的配置选项 更多参数详见参考文档https://npmjs.org/doc/json.html ...

  3. Android之Http通信——3.Android HTTP请求方式:HttpURLConnection

    3.Android HTTP请求方式之HttpURLConnection 引言: 好了,前两节我们已经对HTTP协议进行了学习.相信看完前两节的朋友对HTTP协议相比之前 应该更加熟悉吧.好吧.学了要 ...

  4. vim ctl+v批量添加/删除

    vim编辑器---批量注释与反注释 在使用vim编写代码的时候,经常需要用到批量注释与反注释一段代码.下面简要介绍其操作. 方法一 块选择模式 插入注释: 用v进入virtual模式 用上下键选中需要 ...

  5. Windows Phone开发(42):缓动动画

    原文:Windows Phone开发(42):缓动动画 前面在讨论关键帧动画的时候,我有意把几个带缓动动画的关键帧动画忽略掉,如EasingColorKeyFrame.EasingDoubleKeyF ...

  6. 数据结构 《18》----RMQ 与 LCA 的等价性 (一)

    前言     RMQ: 数组 a0, a1, a2,..., an-1, 中求随意区间 a[i+1], a[i+2], ..., a[i+k] 的最小值     LCA: 求二叉树中两个节点的最低公共 ...

  7. 采用大杀招QEMU调试Linux内核代码

    Linux调试内核代码是非常麻烦.它们一般加printk, 或者使用JTAG调试. 这里的方法是使用QEMU为了调试Linux核心. 由于QEMU自己实现gdb server, 它可以容易地使用gdb ...

  8. 从字节码层面看“HelloWorld” (转)

    一.HelloWorld 字节码生成 众所周知,Java 程序是在 JVM 上运行的,不过 JVM 运行的其实不是 Java 语言本身,而是 Java 程序编译成的字节码文件.可能一开始 JVM 是为 ...

  9. Linux使用快捷键,who命令,rm命令,ps命令,cd,命令kill命令,find命令,grep命令,tar命令(gz、tar、bz2),用户管理,vim配置的一部分,相关命令

    1.进入Ubuntu开场后的终端窗口的快捷键是:           ctrl + alt+t:通过这个命令能够打开终端. ctrl + alt+t:通过这个命令能够打开终端. 再开一个tab选项卡式 ...

  10. WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(一)概要设计

      在我工作的项目中含有多种操作系统.多种设备.多种开发语言,因此需要使用跨平台的通信技术和自定义的消息编码.经过技术调研,ZeroMQ+ProtocolBuffer最终成为通信技术和编码方式.但是如 ...