Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 ImageView 搭配 AnimationDrawable 打造 ProgressBar 旋轉效果

編輯:關於 Android 程式設計

ProgressBar 大家都不陌生,XML 裡一引用、加幾個屬性就能跑。但每個 App 都有自己的視覺調性,直接用系統預設那張轉圈圖,總覺得和整體風格不搭。

自定義 ProgressBar 的樣式其實不難,大致三步:

一、在 anim 目錄下用 animation-list 定義動畫集

 <?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="https://schemas.android.com/apk/res/android"> <item android:duration="50" android:drawable="@drawable/circle_10001" /> <item android:duration="50" android:drawable="@drawable/circle_10002" /> <item android:duration="50" android:drawable="@drawable/circle_10003" /> <item android:duration="50" android:drawable="@drawable/circle_10004" /> <item android:duration="50" android:drawable="@drawable/circle_10005" /> <item android:duration="50" android:drawable="@drawable/circle_10006" /> <item android:duration="50" android:drawable="@drawable/circle_10007" /> </animation-list> 

二、在 style.xml 中定義樣式

 <style name="CircleProgressStyle" parent="@android:style/Widget.ProgressBar.Large"> <item name="android:indeterminateDrawable">@anim/anim_progress_circle</item> </style> 

三、在使用的佈局中套用 style

 <ProgressBar android:layout_width="50dip" android:layout_height="50dip" /> 

這是我長期沿用的寫法,換其他方式也做得到同樣效果。本文重點是另一條路:用 AnimationDrawable 搭配 ImageView 來實現,所以上面就不展開了。

正題:AnimationDrawable + ImageView

先看工程結構:

在 drawable 目錄下新建 circle.xml:

 <?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="https://schemas.android.com/apk/res/android"> <item android:duration="50" android:drawable="@drawable/circle_10001" /> <item android:duration="50" android:drawable="@drawable/circle_10002" /> <item android:duration="50" android:drawable="@drawable/circle_10003" /> <item android:duration="50" android:drawable="@drawable/circle_10004" /> <item android:duration="50" android:drawable="@drawable/circle_10005" /> <item android:duration="50" android:drawable="@drawable/circle_10006" /> <item android:duration="50" android:drawable="@drawable/circle_10007" /> <item android:duration="50" android:drawable="@drawable/circle_10008" /> <item android:duration="50" android:drawable="@drawable/circle_10009" /> <item android:duration="50" android:drawable="@drawable/circle_10010" /> <item android:duration="50" android:drawable="@drawable/circle_10011" /> <item android:duration="50" android:drawable="@drawable/circle_10012" /> <item android:duration="50" android:drawable="@drawable/circle_10013" /> <item android:duration="50" android:drawable="@drawable/circle_10014" /> <item android:duration="50" android:drawable="@drawable/circle_10015" /> </animation-list> 

15 幀、每幀 50 毫秒,一圈約 0.75 秒。oneshot="false" 表示無限循環。

main.xml 做個簡單佈局:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/circle" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="start" android:text="旋轉" /> </LinearLayout> 

MainActivity:

 public class MainActivity extends Activity { private ImageView imageView; private AnimationDrawable animationDrawable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initWedgits(); } /** 初始化各組件 */ private void initWedgits() { try { imageView = (ImageView) findViewById(R.id.imageview); animationDrawable = (AnimationDrawable) imageView.getDrawable(); } catch (Exception e) { e.printStackTrace(); } } public void start(View view) { try { animationDrawable.start(); } catch (Exception e) { e.printStackTrace(); } } }

跑起來之後:沒按按鈕時是一張靜態圖,按下去就一直轉,視覺上和 ProgressBar 等價,而且不必再寫 style。

踩過的坑:start() 該放在哪裡

細心的人會問:為什麼 animationDrawable.start() 要放在 start() 方法裡,而不是直接寫進 initWedgits()?我們試試看:

 public class MainActivity extends Activity { private ImageView imageView; private AnimationDrawable animationDrawable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initWedgits(); } /** 初始化各組件 */ private void initWedgits() { try { imageView = (ImageView) findViewById(R.id.imageview); animationDrawable = (AnimationDrawable)imageView.getDrawable(); animationDrawable.start(); } catch (Exception e) { e.printStackTrace(); } } public void start(View view) { try { // animationDrawable.start(); } catch (Exception e) { e.printStackTrace(); } } } 

理所當然地以為「這樣就會自動轉」,結果運行之後圖片紋絲不動。

接下來幾乎每個人都會走同一條路:懷疑寫錯了,從頭到尾檢查一遍,沒問題;懷疑是沒刷新,把 View 的各种 invalidate 都試一遍,還是不動;懷疑 onCreate 時機不對,改放到 onResume(),仍然不動;最後把 start() 挪進 onWindowFocusChanged(boolean hasFocus)——這次終於轉了。

翻官方文件才找到答案,大意是:不要在 onCreate 中呼叫 AnimationDrawable 的 start(),因為此時 AnimationDrawable 還沒有完全 attach 到視窗上;若想讓動畫自動播放而不依賴點擊等事件觸發,就應該在 onWindowFocusChanged 中呼叫。

所以正確寫法是:

 public class MainActivity extends Activity { private ImageView imageView; private AnimationDrawable animationDrawable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initWedgits(); } /** 初始化各組件 */ private void initWedgits() { try { imageView = (ImageView) findViewById(R.id.imageview); animationDrawable = (AnimationDrawable) imageView.getDrawable(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(hasFocus) animationDrawable.start(); } public void start(View view) { try { //animationDrawable.start(); } catch (Exception e) { e.printStackTrace(); } } } 

另外還有一種更簡潔的做法:在 imageView.post() 裡呼叫 start(),實測同樣有效,不必動用 onWindowFocusChanged。

實務上要留意的一點:onWindowFocusChanged 會在每次視窗獲得焦點時被呼叫,例如使用者下拉通知列再返回、或從其他 App 切回來。反覆呼叫 start() 可能導致動畫從頭重跑而出現跳幀。若在意這點,可以在動畫已在運行時跳過,或者改用 post() 方案。

到此,用 ImageView 與 AnimationDrawable 實現 ProgressBar 效果的說明就完成了。

源碼下載:https://xiazai.jb51.net/201606/yuanma/Android-ImageView-ProgressBar(jb51.net).rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved