Android 动画初步

来源:互联网 发布:哪里有卖呼死你软件 编辑:程序博客网 时间:2024/04/28 07:54

参考博客:http://blog.csdn.net/yanbober/article/details/46481171

Android系统提供了很多丰富的API去实现UI的2D与3D动画,最主要的划分可以分为如下几类:

    1.View Animation: 视图动画在古老的Android版本系统中就已经提供了,只能被用来设置View的动画。

    2.Drawable Animation: 这种动画(也叫Frame动画、帧动画)其实可以划分到视图动画的类别,专门用来一个一个的显示Drawable的

resources,就像放幻灯片一样。

    3.Property Animation: 属性动画只对Android 3.0(API 11)以上版本的Android系统才有效,这种动画可以设置给任何Object,包括

那些还没有渲染到屏幕上的对象。这种动画是可扩展的,可以让你自定义任何类型和属性的动画。

一.View Animation(视图动画)使用
视图动画概述
视图动画,也叫Tween(补间)动画可以在一个视图容器内执行一系列简单变换(位置、大小、旋转、透明度)。譬如,如果你有一个TextView

对象,您可以移动(TranslateAnimation)、旋转(RotateAnimation)、缩放(ScaleAnimation)、透明度(AlphaAnimation)设置其文本,当然,

如果它有一个背景图像,背景图像会随着文本变化。
使用过程:在res下创建anim文件夹 新建一个view_animation.xml 文件,我们定义的补间动画的模板如下:
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator="true">   //android:shareInterpolator="fasle"
    <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>
然后view控件就可以使用这些动画属性:
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
imageview.setAnimation(hyperspaceJumpAnimation);

二.Drawable Animation(Drawable动画)使用详解
Drawable动画概述
Drawable动画其实就是Frame动画(帧动画),它允许你实现像播放幻灯片一样的效果,这种动画的实质其实是Drawable,所以这种动画的XML

定义方式文件一般放在res/drawable/目录下。
使用方法:res 文件夹下面创建drawable 文件夹,创建drawable_animation.xml 文件 定义如下内容
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true"  >
    <item
        android:drawable="@drawable/ic_launcher"
        android:duration="500"
        ></item>   
    <item
        android:drawable="@drawable/video_delete"
        android:duration="500"
        ></item>
    <item
        android:drawable="@drawable/video_playing"
        android:duration="500"
        ></item>
</animation-list>
注意:
<animation-list> 必须是根节点,包含一个或者多个<item>元素,属性有:

    android:oneshot true代表只执行一次,false循环执行。
    <item> 类似一帧的动画资源。

<item> animation-list的子项,包含属性如下:

    android:drawable 一个frame的Drawable资源。
    android:duration 一个frame显示多长时间。

然后在相关view控件设置属性:android:src="@drawable/drawable_animation"
代码中设置动画启动:
                ImageView imageview = (ImageView) findViewById(R.id.show_imageview);
                imageview.setBackgroundResource(R.drawable.ic_launcher);
                AnimationDrawable rocketAnimation = (AnimationDrawable) imageview.getDrawable();
                rocketAnimation.start();

三.Property Animation(属性动画)使用详解
属性动画概述
Android 3.0以后引入了属性动画,属性动画可以轻而易举的实现许多View动画做不到的事,上面也看见了,View动画无非也就做那几种事情,别的也搞不定,而属性动画就可以的,譬如3D旋转一张图片。其实说白了,你记住一点就行,属性动画实现原理就是修改控件的属性值实现的动画。

参考弘扬的博客:http://blog.csdn.net/lmj623565791/article/details/38067475

0 0
原创粉丝点击