Android的Resource Types说的是:保存在res/anim,res/drawable,res/color,res/layout,res/menu,res/values目录下的文件所涉及的类型。本文只讲Animation.
Animation ResourcesTween animations are saved in
res/anim/
and accessed from the
R.anim
class.
Frame animations are saved in
res/drawable/
and accessed from the
R.drawable
class.
Saved in
res/color/
and accessed from the
R.color
class.
Saved in
res/drawable/
and accessed from the
R.drawable
class.
Saved in
res/layout/
and accessed from the
R.layout
class.
Saved in
res/menu/
and accessed from the
R.menu
class.
Saved in
res/values/
and accessed from the
R.string
,
R.array
, and
R.plurals
classes.
Saved in
res/values/
and accessed from the
R.style
class.
Saved in
res/values/
but each accessed from unique
R
sub-classes (such as
R.bool
,
R.integer
,
R.dimen
, etc.).
Animation Resources
1、分类
Animation分两类:Property Animation和View Animation。
Property Animation:通过修改一个对象的属性值来实现动画,用到的是 Animator 类。
View Animation:其又定义了两种动画框架:Tween animation和Frame animation。
Tween Animation:针对单一图片执行各类转换操作来实现动画,用到的是Animation类。
Frame Animation:通过一组图片按一定顺序展示来实现动画,用到的是AnimationDrawable类。
2、Animator与Animation抽象类的继承关系:
Animator
AnimatorSet
ValueAnimator
ObjectAnimator
TimeAnimator
Animation
ScaleAnimation
TranslateAnimation
AlphaAnimation
RotateAnimation
AnimationSet
3、XML属性规范
Property Animation:
文件位置:res/animator/filename.xml
资源类型: ValueAnimator
, ObjectAnimator
, or AnimatorSet
资源引用:In Java: R.animator.filename
In XML: @[package:]animator/filename
语法:
<set android:ordering=["together" | "sequentially"]> <objectAnimator android:propertyName="string" android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <animator android:duration="int" android:valueFrom="float | int | color" android:valueTo="float | int | color" android:startOffset="int" android:repeatCount="int" android:repeatMode=["repeat" | "reverse"] android:valueType=["intType" | "floatType"]/> <set> ... </set> </set>注:根节点必须为
<set>
,
<objectAnimator>
, or
<animator>
<set> 代表 AnimatorSet.
<objectAnimator> 代表 ObjectAnimator.
<animator> 代表 ValueAnimator.
另,AnimatorSet调用范例(ObjectAnimator与ValueAnimator调用类似):
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator); set.setTarget(myObject); set.start();
View Animation
Tween Animation:
文件位置:res/anim/filename.xml
资源类型: Animation的子类:ScaleAnimation、TranslateAnimation、AlphaAnimation、RotateAnimation or AnimationSet.
资源引用:In Java: R.anim.filename
In XML: @[package:]anim/filename
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@[package:]anim/interpolator_resource" android:shareInterpolator=["true" | "false"] > <alpha android:fromAlpha="float" android:toAlpha="float" /> <scale android:fromXScale="float" android:toXScale="float" android:fromYScale="float" android:toYScale="float" android:pivotX="float" android:pivotY="float" /> <translate android:fromXDelta="float" android:toXDelta="float" android:fromYDelta="float" android:toYDelta="float" /> <rotate android:fromDegrees="float" android:toDegrees="float" android:pivotX="float" android:pivotY="float" /> <set> ... </set> </set>注:根节点必须为
<alpha>
,
<scale>
,
<translate>
,
<rotate>
, or
<set>
<alpha> 代表 AlphaAnimation.
<scale> 代表 ScaleAnimation.
<translate> 代表 TranslateAnimation.
<rotate> 代表 RotateAnimation.
<set> 代表 AnimationSet.
另,AnimationSet调用范例(其他Animation调用一致):
ImageView image = (ImageView) findViewById(R.id.image); Animation hyperspaceJump = AnimationUtils.系统提供的加速器:loadAnimation
(this, R.anim.hyperspace_jump); image.startAnimation
(hyperspaceJump);
Interpolator class | Resource ID |
---|---|
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator |
AccelerateInterpolator | @android:anim/accelerate_interpolator |
AnticipateInterpolator | @android:anim/anticipate_interpolator |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator |
BounceInterpolator | @android:anim/bounce_interpolator |
CycleInterpolator | @android:anim/cycle_interpolator |
DecelerateInterpolator | @android:anim/decelerate_interpolator |
LinearInterpolator | @android:anim/linear_interpolator |
OvershootInterpolator | @android:anim/overshoot_interpolator |
Frame Animation
文件位置:res/drawable/filename.xml
资源类型: AnimationDrawable
资源引用:In Java: R.drawable.filename
In XML: @[package:]drawable.filename
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" /> </animation-list>调用范例:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); rocketImage.setBackgroundResource
(R.drawable.rocket_thrust); rocketAnimation = (AnimationDrawable) rocketImage.getBackground()
; rocketAnimation.start()
;