Android圖片滾動,加入自動播放功能,使用自定義屬性實現,霸氣十足!

轉自: http://blog.csdn.net/guolin_blog/article/details/8796877


轉載請註明出處:http://blog.csdn.net/guolin_blog/article/details/8796877

大家好,記得上次我帶着大家一起實現了一個類似與淘寶客戶端中帶有的圖片滾動播放器的效果,但是在做完了之後,發現忘了加入圖片自動播放的功能(或許是我有意忘記加的.....),結果圖片只能通過手指滑動來播放。於是今天我將再次帶領大家,添加上之前遺漏的功能,讓我們的圖片播放器更加完善。

這次的程序開發將完全基於上一次的代碼,如果有朋友還未看過上篇文章,請先閱讀 Android實現圖片滾動控件,含頁籤功能,讓你的應用像淘寶一樣炫起來 

既然是要加入自動播放的功能,那麼就有一個非常重要的問題需要考慮。如果當前已經滾動到了最後一張圖片,應該怎麼辦?由於我們目前的實現方案是,所有的圖片都按照佈局文件裏面定義的順序橫向排列,然後通過偏移第一個圖片的leftMargin,來決定顯示哪一張圖片。因此當圖片滾動在最後一張時,我們可以讓程序迅速地回滾到第一張圖片,然後從頭開始滾動。這種效果和淘寶客戶端是有一定差異的(淘寶並沒有回滾機制,而是很自然地由最後一張圖片滾動到第一張圖片),我也研究過淘寶圖片滾動器的實現方法,並不難實現。但是由於我們是基於上次的代碼進行開發的,方案上無法實現和淘寶客戶端一樣的效果,因此這裏也就不追求和它完全一致了,各有風格也挺好的。

好了,現在開始實現功能,首先是打開SlidingSwitcherView,在裏面加入一個新的AsyncTask,專門用於回滾到第一張圖片:

[java] view plaincopy
  1. class ScrollToFirstItemTask extends AsyncTask<Integer, Integer, Integer> {  
  2.   
  3.     @Override  
  4.     protected Integer doInBackground(Integer... speed) {  
  5.         int leftMargin = firstItemParams.leftMargin;  
  6.         while (true) {  
  7.             leftMargin = leftMargin + speed[0];  
  8.             // 當leftMargin大於0時,說明已經滾動到了第一個元素,跳出循環  
  9.             if (leftMargin > 0) {  
  10.                 leftMargin = 0;  
  11.                 break;  
  12.             }  
  13.             publishProgress(leftMargin);  
  14.             sleep(20);  
  15.         }  
  16.         return leftMargin;  
  17.     }  
  18.   
  19.     @Override  
  20.     protected void onProgressUpdate(Integer... leftMargin) {  
  21.         firstItemParams.leftMargin = leftMargin[0];  
  22.         firstItem.setLayoutParams(firstItemParams);  
  23.     }  
  24.   
  25.     @Override  
  26.     protected void onPostExecute(Integer leftMargin) {  
  27.         firstItemParams.leftMargin = leftMargin;  
  28.         firstItem.setLayoutParams(firstItemParams);  
  29.     }  
  30.   
  31. }  
然後在SlidingSwitcherView裏面加入一個新的方法:
[java] view plaincopy
  1. /** 
  2.  * 滾動到第一個元素。 
  3.  */  
  4. public void scrollToFirstItem() {  
  5.     new ScrollToFirstItemTask().execute(20 * itemsCount);  
  6. }  
這個方法非常簡單,就是啓動了我們新增的ScrollToFirstItemTask,滾動速度設定爲20 * itemsCount,這樣當我們需要滾動的圖片數量越多,回滾速度就會越快。定義好這個方法後,只要在任意地方調用scrollToFirstItem這個方法,就可以立刻從當前圖片回滾到第一張圖片了。

OK,然後我們要定義一個方法用於啓動自動播放功能。仍然是在SlidingSwitcherView中新增如下代碼:

[java] view plaincopy
  1. /** 
  2.  * 用於在定時器當中操作UI界面。 
  3.  */  
  4. private Handler handler = new Handler();  
  5.   
  6. /** 
  7.  * 開啓圖片自動播放功能,當滾動到最後一張圖片的時候,會自動回滾到第一張圖片。 
  8.  */  
  9. public void startAutoPlay() {  
  10.     new Timer().scheduleAtFixedRate(new TimerTask() {  
  11.         @Override  
  12.         public void run() {  
  13.             if (currentItemIndex == itemsCount - 1) {  
  14.                 currentItemIndex = 0;  
  15.                 handler.post(new Runnable() {  
  16.                     @Override  
  17.                     public void run() {  
  18.                         scrollToFirstItem();  
  19.                         refreshDotsLayout();  
  20.                     }  
  21.                 });  
  22.             } else {  
  23.                 currentItemIndex++;  
  24.                 handler.post(new Runnable() {  
  25.                     @Override  
  26.                     public void run() {  
  27.                         scrollToNext();  
  28.                         refreshDotsLayout();  
  29.                     }  
  30.                 });  
  31.             }  
  32.         }  
  33.     }, 30003000);  
  34. }  
我們可以看到,這個方法裏啓用了一個定時器,每隔三秒就會執行一次。然後在定時器的執行邏輯裏面進行判斷當前圖片是否是最後一張,如果不是最後一張就滾動到下一張圖片,如果是最後一張就回滾到第一張圖片。其中需要注意,定時器中的代碼是在子線程中運行的,而滾動圖片操作和更新頁籤操作都是UI操作,因此需要放到Handler中去執行。

之後只要在Activity創建的時候去調用SlidingSwitcherView的startAutoPlay方法,自動播放功能就實現了!!

結束了?Naive!!  如果就這麼結束了,怎麼對得起大家的期待,如此簡單的功能還要用一篇文章來講簡直是弱爆了。

接下來纔是今天的重點,我們要使用自定義屬性來啓用自動播放功能,這樣才能讓你更加接近高手,才能讓你更加玩轉Android。

那我們繼續,在res/values目錄下新建一個attrs.xml文件,裏面加入代碼:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <resources>  
  3.   
  4.     <attr name="auto_play" forMymat="boolean" />  
  5.   
  6.     <declare-styleable name="SlidingSwitcherView">  
  7.         <attr name="auto_play" />  
  8.     </declare-styleable>  
  9.   
  10. </resources>  
其中,auto_play是我們將要使用的屬性名,格式是布爾型。SlidingSwitcherView這個值可以隨意,主要在代碼中需要引用相應的id。

然後重寫SlidingSwitcherView的構造函數,在裏面加入從佈局文件中獲取自定義屬性的代碼:

[java] view plaincopy
  1. public SlidingSwitcherView(Context context, AttributeSet attrs) {  
  2.     super(context, attrs);  
  3.     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingSwitcherView);  
  4.     boolean isAutoPlay = a.getBoolean(R.styleable.SlidingSwitcherView_auto_play, false);  
  5.     if (isAutoPlay) {  
  6.         startAutoPlay();  
  7.     }  
  8.     a.recycle();  
  9. }  
可以看到,我們在構造函數中去獲取auto_play的值,如果爲true,就調用startAutoPlay方法,從而啓用了自動播放的功能。

接下來就是見證奇蹟的時刻!讓我們打開activity_main.xml,在裏面加入兩行關鍵性代碼。在最外層的LinearLayout加入xmlns:myattr="http://schemas.android.com/apk/res/com.example.viewswitcher"。在我們自定義的com.example.viewswitcher.SlidingSwitcherView加入myattr:auto_play="true"。完整XML代碼如下:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:myattr="http://schemas.android.com/apk/res/com.example.viewswitcher"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <com.example.viewswitcher.SlidingSwitcherView  
  10.         android:id="@+id/slidingLayout"  
  11.         myattr:auto_play="true"  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="100dip" >  
  14.   
  15.         <LinearLayout  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent"  
  18.             android:orientation="horizontal" >  
  19.   
  20.             <Button  
  21.                 android:layout_width="fill_parent"  
  22.                 android:layout_height="fill_parent"  
  23.                 android:background="@drawable/image1" />  
  24.   
  25.             <Button  
  26.                 android:layout_width="fill_parent"  
  27.                 android:layout_height="fill_parent"  
  28.                 android:background="@drawable/image2" />  
  29.   
  30.             <Button  
  31.                 android:layout_width="fill_parent"  
  32.                 android:layout_height="fill_parent"  
  33.                 android:background="@drawable/image3" />  
  34.   
  35.             <Button  
  36.                 android:layout_width="fill_parent"  
  37.                 android:layout_height="fill_parent"  
  38.                 android:background="@drawable/image4" />  
  39.         </LinearLayout>  
  40.   
  41.         <LinearLayout  
  42.             android:layout_width="60dip"  
  43.             android:layout_height="20dip"  
  44.             android:layout_alignParentBottom="true"  
  45.             android:layout_alignParentRight="true"  
  46.             android:layout_margin="15dip"  
  47.             android:orientation="horizontal" >  
  48.         </LinearLayout>  
  49.     </com.example.viewswitcher.SlidingSwitcherView>  
  50.       
  51. </LinearLayout>  
也就是說,我們只需要通過設定myattr:auto_play是等於true還是false,就可以決定是否啓用自動播放功能,非常簡單方便。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章