Android 用Animation-list实现逐帧动画 (转载)

转自:http://blog.csdn.net/aminfo/article/details/7847761

第一步:先上图片素材,以下素材放到res/drawable目录下:

http://blog.csdn.net/aminfo/article/details/7847761

图片素材:                                     

文件名称:icon1.png      icon2.png       icon3.png        icon4.png       icon5.png      icon6.png

第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,一个是倒序显示动画,文件存放在res/drawable目录下

顺序显示动画文件:animation1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--
        根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
        根标签下,通过item标签对动画中的每一个图片进行声明
        android:duration 表示展示所用的该图片的时间长度
     -->
    <animation-list
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:oneshot="true"
      >
        <item android:drawable="@drawable/icon1" android:duration="150"></item>
        <item android:drawable="@drawable/icon2" android:duration="150"></item>
        <item android:drawable="@drawable/icon3" android:duration="150"></item>
        <item android:drawable="@drawable/icon4" android:duration="150"></item>
        <item android:drawable="@drawable/icon5" android:duration="150"></item>
        <item android:drawable="@drawable/icon6" android:duration="150"></item>
    </animation-list>  

倒序显示动画文件:animation2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--
        根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
        根标签下,通过item标签对动画中的每一个图片进行声明
        android:duration 表示展示所用的该图片的时间长度
     -->
    <animation-list
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:oneshot="true"
      >
        <item android:drawable="@drawable/icon6" android:duration="150"></item>
        <item android:drawable="@drawable/icon5" android:duration="150"></item>
        <item android:drawable="@drawable/icon4" android:duration="150"></item>
        <item android:drawable="@drawable/icon3" android:duration="150"></item>
        <item android:drawable="@drawable/icon2" android:duration="150"></item>
        <item android:drawable="@drawable/icon1" android:duration="150"></item>
    </animation-list>  

第三步:上布局文件,放在res/layout目录下,文件名main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical">  

        <ImageView android:id="@+id/animationIV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5px"
                android:src="@drawable/animation1"/>   

        <Button android:id="@+id/buttonA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="顺序显示" />  

        <Button android:id="@+id/buttonB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="停止" />  

        <Button android:id="@+id/buttonC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:text="倒序显示" />  

    </LinearLayout>  

第四步:上Activity文件,文件名:MainActivity.java

    package org.shuxiang.test;  

    import android.app.Activity;
    import android.graphics.drawable.AnimationDrawable;  

    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.ImageView;  

    public class Activity10 extends Activity
    {
        private ImageView animationIV;
        private Button buttonA, buttonB, buttonC;
        private AnimationDrawable animationDrawable;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.test10);  

            animationIV = (ImageView) findViewById(R.id.animationIV);
            buttonA = (Button) findViewById(R.id.buttonA);
            buttonB = (Button) findViewById(R.id.buttonB);
            buttonC = (Button) findViewById(R.id.buttonC);  

            buttonA.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    animationIV.setImageResource(R.drawable.animation1);
                    animationDrawable = (AnimationDrawable) animationIV.getDrawable();
                    animationDrawable.start();
                }  

            });   

            buttonB.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    animationDrawable = (AnimationDrawable) animationIV.getDrawable();
                    animationDrawable.stop();
                }  

            });  

            buttonC.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    animationIV.setImageResource(R.drawable.animation2);
                    animationDrawable = (AnimationDrawable) animationIV.getDrawable();
                    animationDrawable.start();
                }
            });
        }
    }  
时间: 2024-08-03 13:48:09

Android 用Animation-list实现逐帧动画 (转载)的相关文章

Android Animation 动画Demo(Frame逐帧动画)

上一篇介绍了Animation动画其一:Tween补间动画. 这篇文章接下来介绍Animation另一种动画形式:Frame逐帧动画. Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画.Frame动画可以被定义在XML文件中,也可以完全编码实现(后面会给出这两种实现方式的源代码Demo). 下面分别介绍: 一.定义在xml中实现: 实现效果图: 源代码: 布局文件:main.xml: <?xml version="1.0" encodin

Android动画效果之Frame Animation(逐帧动画)(二)(

前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame Animation(逐帧动画). Frame Animation(逐帧动画): 逐帧动画(Frame-by-frame Animations)从字面上理解就是一帧挨着一帧的播放图片,就像放电影一样.和补间动画一样可以通过xml实现也可以通过java代码实现.接下来借助目前项目中的一个开奖的动画来总结

Android动画效果之Frame Animation(逐帧动画)

前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame Animation(逐帧动画). 其他几种动画效果: Android动画效果之Tween Animation(补间动画) Android动画效果之Frame Animation(逐帧动画) Android动画效果之初识Property Animation(属性动画) Android动画效果之Prop

Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. android提供两种方法为AnimationDrawable添加帧:XML定义和JAVA代码创建. XML 因为动画帧的资源需要是一个Drawable对象,所以需要把它放到Drawable目录下.在<animation-list>使用<item>来添加一帧 anima.xml <?xml

android 逐帧动画的播放与播放结束操作对象

逐帧动画的xml代码: 1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:oneshot="true" > 4 5 <item 6 android:drawable="@drawabl

Android中的动画具体解释系列【1】——逐帧动画

逐帧动画事实上非常easy,以下我们来看一个样例: <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawa

Android -- 逐帧动画

在处理耗时工作的时候,大多数会弹出一个加载的框,里面有一个连续旋转的图片,很多时候都是用一张图片,使用rotate来设定旋转,不过看起来不太美观,没有形象感,在3.0之前Android有两种动画效果分别是补间动画和帧动画,用一张图片实现的是使用补间动画,定义给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变. 动画布局                                                                               

Android 学习之逐帧动画(Frame)

帧动画就是将一些列图片.依次播放. 利用肉眼的"视觉暂留"的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的. a:须要定义逐帧动画,能够通过代码定义.也能够通过XML文件定义.一般XML文件定义比較直观 <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/

转Android 用Animation-list实现逐帧动画

Android 用Animation-list实现逐帧动画 第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: icon1.png icon2.png icon3.png icon4.png icon5.png icon6.png 第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,一个是倒序显示动画,文件存放在res/d

css3 animation实现逐帧动画

css3里面的animation属性非常强大,但是自己用的比较少,最近有次面试就刚好被问到了,趁现在有时间就对animation做一个小总结.同时实现一个逐帧动画的demo作为练习 animation属性一览 因为animation属性比较多,然后在w3c上看有点蛋疼,干脆也做了一份导图,以后想查看,就一目了然了 使用animation实现逐帧动画 熟悉了animation的属性之后,得找个简单的小项目实现下,逐帧动画好有意思,先跑一个满足下自己思路很简单,就是给元素一个雪碧图的背景,然后添加的