Android学习笔记进阶十一图片动画播放(AnimationDrawable)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android学习笔记进阶十一图片动画播放(AnimationDrawable)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                大家平時見到的最多的可能就是Frame動畫了,Android中當然也少不了它。它的使用更加簡單,只需要創建一個
AnimationDrawabledF對象來表示Frame動畫,然后通過addFrame 方法把每一幀要顯示的內容添加進去,并設置播放間隔時間,本例子中間隔時間為5S,
最后通過start 方法就可。
以播放這個動畫了,同時還可以通過 setOneShot方法設置是否重復播放。
[java] view plaincopy- package?xiaosi.bu;??
- ??
- import?android.app.Activity;??
- import?android.graphics.drawable.AnimationDrawable;??
- import?android.os.Bundle;??
- import?android.view.View;??
- import?android.view.View.OnClickListener;??
- import?android.widget.Button;??
- import?android.widget.ImageView;??
- ??
- public?class?TupianActivity?extends?Activity?{??
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????private?Button?start?=?null;??
- ????private?Button?stop?=?null;??
- ????private?ImageView?image?=?null;??
- ????private?AnimationDrawable?animationDrawable?=?null;??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ??????????
- ????????start?=?(Button)findViewById(R.id.start);??
- ????????start.setOnClickListener(new?StartListener());??
- ????????stop?=?(Button)findViewById(R.id.stop);??
- ????????stop.setOnClickListener(new?StopListener());??
- ??????????
- ????????image?=?(ImageView)findViewById(R.id.imageview);??
- ??????????
- ????????animationDrawable?=?new?AnimationDrawable();??
- ????????for(int?i?=0;i<8;i++){??
- ????????????//第一個?就是我們的資源名稱(圖片名)????
- ????????????//第二個?就是我們存放圖片的文件夾drawable????
- ????????????//第三個?包名也可以用Context的getPackageName返回應用程序的包名????
- ????????????int?id?=?getResources().getIdentifier(?"a"+i,?"drawable",?"xiaosi.bu");??
- ????????????System.out.println("ID:"?+?id);??
- ????????????animationDrawable.addFrame(getResources().getDrawable(id),?2000);??
- ????????}??
- ??????//設置手否重復播放,false為重復??
- ????????animationDrawable.setOneShot(false);??
- ????????image.setImageDrawable(animationDrawable);??
- ??
- ????}??
- ????private?class?StartListener?implements?OnClickListener{??
- ??
- ????????public?void?onClick(View?v)??
- ????????{??
- ????????????animationDrawable.start();??
- ????????}??
- ????}??
- ??????
- ????private?class?StopListener?implements?OnClickListener{??
- ??
- ????????public?void?onClick(View?v)??
- ????????{??
- ????????????animationDrawable.stop();????
- ????????}??
- ????}??
- }??
 main.xml
- <?xml?version="1.0"?encoding="utf-8"?>??
- ????<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????????android:orientation="vertical"???
- ????????android:layout_width="fill_parent"??
- ???????android:layout_height="fill_parent">??
- ???????<LinearLayout??
- ????????android:orientation="horizontal"???
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content">??
- ???????<Button?android:id="@+id/start"??
- ???????????android:text="Start"???
- ???????????android:layout_width="wrap_content"??
- ???????????android:layout_height="wrap_content"/>??
- ???????<Button?android:id="@+id/stop"??
- ???????????android:text="End"??
- ???????????android:layout_width="wrap_content"??
- ???????????android:layout_height="wrap_content"/>??
- ???????</LinearLayout>??
- ???????<ImageView?android:id="@+id/imageview"???
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="fill_parent"???
- ???????????android:scaleType="fitXY"???
- ???????????android:background="#ffffff"?/>??
- ?</LinearLayout>??
 ?
?
源代碼:點擊打開鏈接
轉載于:https://www.cnblogs.com/Free-Thinker/p/6721791.html
總結
以上是生活随笔為你收集整理的Android学习笔记进阶十一图片动画播放(AnimationDrawable)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 自行车的碟刹要多少钱
- 下一篇: 花好月圆原唱是谁唱的啊?
