日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Android学习笔记进阶十一图片动画播放(AnimationDrawable)

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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>??


?

?

源代碼:點擊打開鏈接

轉載于:https://www.cnblogs.com/Free-Thinker/p/6721791.html

總結

以上是生活随笔為你收集整理的Android学习笔记进阶十一图片动画播放(AnimationDrawable)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。