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

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

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

    最后通过start 方法就可。

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

    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

    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>  


     

    源代码:点击打开链接

  • 相关阅读:
    Eclipse 导入项目乱码问题(中文乱码)
    sql中视图视图的作用
    Java基础-super关键字与this关键字
    Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
    Android View和ViewGroup
    工厂方法模式(java 设计模式)
    设计模式(java) 单例模式 单例类
    eclipse乱码解决方法
    No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案
    【转】使用 Eclipse 调试 Java 程序的 10 个技巧
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6721791.html
Copyright © 2011-2022 走看看