zoukankan      html  css  js  c++  java
  • Android学习笔记进阶十一图片动画播放(AnimationDrawable)

    大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个

    AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,

    最后通过start 方法就可。

    以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。

    [java] view plaincopy
     
    1. package xiaosi.bu;  
    2.   
    3. import android.app.Activity;  
    4. import android.graphics.drawable.AnimationDrawable;  
    5. import android.os.Bundle;  
    6. import android.view.View;  
    7. import android.view.View.OnClickListener;  
    8. import android.widget.Button;  
    9. import android.widget.ImageView;  
    10.   
    11. public class TupianActivity extends Activity {  
    12.     /** Called when the activity is first created. */  
    13.     private Button start = null;  
    14.     private Button stop = null;  
    15.     private ImageView image = null;  
    16.     private AnimationDrawable animationDrawable = null;  
    17.     @Override  
    18.     public void onCreate(Bundle savedInstanceState) {  
    19.         super.onCreate(savedInstanceState);  
    20.         setContentView(R.layout.main);  
    21.           
    22.         start = (Button)findViewById(R.id.start);  
    23.         start.setOnClickListener(new StartListener());  
    24.         stop = (Button)findViewById(R.id.stop);  
    25.         stop.setOnClickListener(new StopListener());  
    26.           
    27.         image = (ImageView)findViewById(R.id.imageview);  
    28.           
    29.         animationDrawable = new AnimationDrawable();  
    30.         for(int i =0;i<8;i++){  
    31.             //第一个 就是我们的资源名称(图片名)    
    32.             //第二个 就是我们存放图片的文件夹drawable    
    33.             //第三个 包名也可以用Context的getPackageName返回应用程序的包名    
    34.             int id = getResources().getIdentifier( "a"+i, "drawable""xiaosi.bu");  
    35.             System.out.println("ID:" + id);  
    36.             animationDrawable.addFrame(getResources().getDrawable(id), 2000);  
    37.         }  
    38.       //设置手否重复播放,false为重复  
    39.         animationDrawable.setOneShot(false);  
    40.         image.setImageDrawable(animationDrawable);  
    41.   
    42.     }  
    43.     private class StartListener implements OnClickListener{  
    44.   
    45.         public void onClick(View v)  
    46.         {  
    47.             animationDrawable.start();  
    48.         }  
    49.     }  
    50.       
    51.     private class StopListener implements OnClickListener{  
    52.   
    53.         public void onClick(View v)  
    54.         {  
    55.             animationDrawable.stop();    
    56.         }  
    57.     }  
    58. }  


    main.xml

    [java] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.         android:orientation="vertical"   
    4.         android:layout_width="fill_parent"  
    5.        android:layout_height="fill_parent">  
    6.        <LinearLayout  
    7.         android:orientation="horizontal"   
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content">  
    10.        <Button android:id="@+id/start"  
    11.            android:text="Start"   
    12.            android:layout_width="wrap_content"  
    13.            android:layout_height="wrap_content"/>  
    14.        <Button android:id="@+id/stop"  
    15.            android:text="End"  
    16.            android:layout_width="wrap_content"  
    17.            android:layout_height="wrap_content"/>  
    18.        </LinearLayout>  
    19.        <ImageView android:id="@+id/imageview"   
    20.             android:layout_width="fill_parent"  
    21.             android:layout_height="fill_parent"   
    22.            android:scaleType="fitXY"   
    23.            android:background="#ffffff" />  
    24.  </LinearLayout>  


     

    源代码:点击打开链接

     转自:http://blog.csdn.net/sjf0115/article/details/7265307

  • 相关阅读:
    如何在Windows 10 IoT Core中添加其他语言的支持,如中文
    交易应用及网站驱动不兼容Windows 10的解决方案
    五步轻松实现对现代浏览器的兼容
    在Windows 10中开启开发者模式
    Modern.IE,创建现代网站的给力开发工具!
    Visual Studio的 Apache Cordova 插件CTP3.0发布!
    什么开发?什么是编程语言?你能学吗?你能做吗?
    IT'S NOT A JOKE!一篇博客让你玩转IT领域!你说我吹NB?请进来跟着浪一浪
    python为什么慢?
    编译错误 expected class-name before ‘{’ token
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2513421.html
Copyright © 2011-2022 走看看