Android Animation动画体系结构

Android动画分为两类动画:Property Animation(属性动画)和View Animation(视图动画)

reference:
http://developer.android.com/guide/topics/resources/animation-resource.html#Property
http://developer.android.com/guide/topics/graphics/prop-animation.html

一、Property Animation(属性动画)
1.适用对象:view objects和 non-view objects。
API Class支持: android.animation

2.通过xml文件实现,放在res/animator目录下面引用。
In Java: R.animator.filename
In XML: @[package:]animator/filename

3.xml语法样式:
<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>

4. Resource DataType
 ValueAnimator, ObjectAnimator, or AnimatorSet.
和xml中标签对应关系:
<set></set>           -->   AnimatonSet
<objectAnimation/>    -->   ObjectAnimator
<animator/>           -->   ValueAnimatior

<set></set>标签中可以放以上三种类型的标签。
<set></set>标签中通过android:ordering指定动画是顺序播放还是同时播放。

5.简单使用
AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,
    R.anim.property_animator);
set.setTarget(myObject);
set.start();

6.reference
http://developer.android.com/guide/topics/resources/animation-resource.html#Property



二、View Animation(视图动画)
View Animation(视图动画)包含两种类型:Tween animation和Frame animation.
Tweened animation is handled by this package (android.view.animation); frame-by-frame animation is handled by the AnimationDrawable class.

reference:
http://developer.android.com/guide/topics/resources/animation-resource.html#View
http://developer.android.com/guide/topics/graphics/view-animation.html

(一) Tween animation
tweened animation, in which you tell Android to perform a series of simple transformations (position, size, rotation, and so on) to the content of a View.

1. 适用对象:view objects
API Class支持:android.view.animation

2.通过xml文件实现,放在res/anim目录下面引用。
In Java: R.anim.filename
In XML: @[package:]anim/filename

3.xml语法样式:
<?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>

4. Resource DataType
 AnimatonSet、AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation.
和xml中标签对应关系:
<set></set>       -->    AnimatonSet
<alpha>           -->    AlphaAnimation
<scale>           -->    ScaleAnimation
<translate>       -->    TranslateAnimation
<rotate>          -->    RotateAnimation

<set></set>标签中可以放以上五种类型的标签。
<set></set>标签中有一个比较重要的属性android:interpolator.

5.简单使用
ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
image.startAnimation(hyperspaceJump);

6. reference
http://developer.android.com/guide/topics/resources/animation-resource.html#Tween


(二) Frame animation
frame-by-frame animation, which loads a series of Drawable resources one after the other.

1. 适用对象:view objects
API Class支持:AnimationDrawable

2.通过xml文件实现,引用方式:res/drawable/filename.xml
In Java: R.drawable.filename
In XML: @[package:]drawable.filename

3.xml语法样式:
<?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>

4. 比较重要的标签
<animation-list> 做为root根标签,包含无数<item>子标签,通过android:oneshot指定重复与否.
<item> 包含在<animation-list>标签之内,包含两个属性:android:drawable和android:duration.

5.简单使用
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);

rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

6. reference
http://developer.android.com/guide/topics/resources/animation-resource.html#Frame



三、Property Animation(属性动画)和View Animation(视图动画)的区别

《How Property Animation Differs from View Animatio》


The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.

The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.

reference:
 http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值