Android使用自定義屬性實(shí)現(xiàn)圖片自動播放滾動的功能
大家好,記得上次我?guī)е蠹乙黄饘?shí)現(xiàn)了一個類似與淘寶客戶端中帶有的圖片滾動播放器的效果,但是在做完了之后,發(fā)現(xiàn)忘了加入圖片自動播放的功能(或許是我有意忘記加的.....),結(jié)果圖片只能通過手指滑動來播放。于是今天我將再次帶領(lǐng)大家,添加上之前遺漏的功能,讓我們的圖片播放器更加完善。
這次的程序開發(fā)將完全基于上一次的代碼,如果有朋友還未看過上篇文章,請先閱讀Android實(shí)現(xiàn)圖片滾動和頁簽控件功能的實(shí)現(xiàn)代碼。
既然是要加入自動播放的功能,那么就有一個非常重要的問題需要考慮。如果當(dāng)前已經(jīng)滾動到了最后一張圖片,應(yīng)該怎么辦?由于我們目前的實(shí)現(xiàn)方案是,所有的圖片都按照布局文件里面定義的順序橫向排列,然后通過偏移第一個圖片的leftMargin,來決定顯示哪一張圖片。因此當(dāng)圖片滾動在最后一張時,我們可以讓程序迅速地回滾到第一張圖片,然后從頭開始滾動。這種效果和淘寶客戶端是有一定差異的(淘寶并沒有回滾機(jī)制,而是很自然地由最后一張圖片滾動到第一張圖片),我也研究過淘寶圖片滾動器的實(shí)現(xiàn)方法,并不難實(shí)現(xiàn)。但是由于我們是基于上次的代碼進(jìn)行開發(fā)的,方案上無法實(shí)現(xiàn)和淘寶客戶端一樣的效果,因此這里也就不追求和它完全一致了,各有風(fēng)格也挺好的。
好了,現(xiàn)在開始實(shí)現(xiàn)功能,首先是打開SlidingSwitcherView,在里面加入一個新的AsyncTask,專門用于回滾到第一張圖片:
class ScrollToFirstItemTask extends AsyncTask<Integer, Integer, Integer> { @Override protected Integer doInBackground(Integer... speed) { int leftMargin = firstItemParams.leftMargin; while (true) { leftMargin = leftMargin + speed[0]; // 當(dāng)leftMargin大于0時,說明已經(jīng)滾動到了第一個元素,跳出循環(huán) if (leftMargin > 0) { leftMargin = 0; break; } publishProgress(leftMargin); sleep(20); } return leftMargin; } @Override protected void onProgressUpdate(Integer... leftMargin) { firstItemParams.leftMargin = leftMargin[0]; firstItem.setLayoutParams(firstItemParams); } @Override protected void onPostExecute(Integer leftMargin) { firstItemParams.leftMargin = leftMargin; firstItem.setLayoutParams(firstItemParams); } }
然后在SlidingSwitcherView里面加入一個新的方法:
/** * 滾動到第一個元素。 */ public void scrollToFirstItem() { new ScrollToFirstItemTask().execute(20 * itemsCount); }
這個方法非常簡單,就是啟動了我們新增的ScrollToFirstItemTask,滾動速度設(shè)定為20 * itemsCount,這樣當(dāng)我們需要滾動的圖片數(shù)量越多,回滾速度就會越快。定義好這個方法后,只要在任意地方調(diào)用scrollToFirstItem這個方法,就可以立刻從當(dāng)前圖片回滾到第一張圖片了。
OK,然后我們要定義一個方法用于啟動自動播放功能。仍然是在SlidingSwitcherView中新增如下代碼:
/** * 用于在定時器當(dāng)中操作UI界面。 */ private Handler handler = new Handler(); /** * 開啟圖片自動播放功能,當(dāng)滾動到最后一張圖片的時候,會自動回滾到第一張圖片。 */ public void startAutoPlay() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { if (currentItemIndex == itemsCount - 1) { currentItemIndex = 0; handler.post(new Runnable() { @Override public void run() { scrollToFirstItem(); refreshDotsLayout(); } }); } else { currentItemIndex++; handler.post(new Runnable() { @Override public void run() { scrollToNext(); refreshDotsLayout(); } }); } } }, 3000, 3000); }
我們可以看到,這個方法里啟用了一個定時器,每隔三秒就會執(zhí)行一次。然后在定時器的執(zhí)行邏輯里面進(jìn)行判斷當(dāng)前圖片是否是最后一張,如果不是最后一張就滾動到下一張圖片,如果是最后一張就回滾到第一張圖片。其中需要注意,定時器中的代碼是在子線程中運(yùn)行的,而滾動圖片操作和更新頁簽操作都是UI操作,因此需要放到Handler中去執(zhí)行。
之后只要在Activity創(chuàng)建的時候去調(diào)用SlidingSwitcherView的startAutoPlay方法,自動播放功能就實(shí)現(xiàn)了?。?/p>
結(jié)束了?Naive!! 如果就這么結(jié)束了,怎么對得起大家的期待,如此簡單的功能還要用一篇文章來講簡直是弱爆了。
接下來才是今天的重點(diǎn),我們要使用自定義屬性來啟用自動播放功能,這樣才能讓你更加接近高手,才能讓你更加玩轉(zhuǎn)Android。
那我們繼續(xù),在res/values目錄下新建一個attrs.xml文件,里面加入代碼:
<?xml version="1.0" encoding="UTF-8"?> <resources> <attr name="auto_play" forMymat="boolean" /> <declare-styleable name="SlidingSwitcherView"> <attr name="auto_play" /> </declare-styleable> </resources>
其中,auto_play是我們將要使用的屬性名,格式是布爾型。SlidingSwitcherView這個值可以隨意,主要在代碼中需要引用相應(yīng)的id。
然后重寫SlidingSwitcherView的構(gòu)造函數(shù),在里面加入從布局文件中獲取自定義屬性的代碼:
public SlidingSwitcherView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingSwitcherView); boolean isAutoPlay = a.getBoolean(R.styleable.SlidingSwitcherView_auto_play, false); if (isAutoPlay) { startAutoPlay(); } a.recycle(); }
可以看到,我們在構(gòu)造函數(shù)中去獲取auto_play的值,如果為true,就調(diào)用startAutoPlay方法,從而啟用了自動播放的功能。
接下來就是見證奇跡的時刻!讓我們打開activity_main.xml,在里面加入兩行關(guān)鍵性代碼。在最外層的LinearLayout加入在我們自定義的com.example.viewswitcher.SlidingSwitcherView加入myattr:auto_play="true"。完整XML代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myattr="http://schemas.android.com/apk/res/com.example.viewswitcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" > <com.example.viewswitcher.SlidingSwitcherView android:id="@+id/slidingLayout" myattr:auto_play="true" android:layout_width="fill_parent" android:layout_height="100dip" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image1" /> <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image2" /> <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image3" /> <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image4" /> </LinearLayout> <LinearLayout android:layout_width="60dip" android:layout_height="20dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="15dip" android:orientation="horizontal" > </LinearLayout> </com.example.viewswitcher.SlidingSwitcherView> </LinearLayout>
也就是說,我們只需要通過設(shè)定myattr:auto_play是等于true還是false,就可以決定是否啟用自動播放功能,非常簡單方便。
好了,今天的講解到此結(jié)束,有疑問的朋友請?jiān)谙旅媪粞浴?/p>
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android自定義Drawable之在Drawable中部指定透明區(qū)域方法示例
對于不同的屏幕密度、不同的設(shè)備方向,不同的語言和區(qū)域,都會涉及到備選 drawable 資源,下面這篇文章主要給你大家介紹了關(guān)于Android自定義Drawable之在Drawable中部指定透明區(qū)域的相關(guān)資料,需要的朋友可以參考下2018-07-07Android實(shí)現(xiàn)GridView中ImageView動態(tài)變換的方法
這篇文章主要介紹了Android實(shí)現(xiàn)GridView中ImageView動態(tài)變換的方法,以實(shí)例形式較為詳細(xì)的分析了GridView中ImageView動態(tài)變換的頁面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2015-10-10Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android之Intent附加數(shù)據(jù)的兩種實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了添加數(shù)據(jù)到Intent的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09Android4.0平板開發(fā)之隱藏底部任務(wù)欄的方法
這篇文章主要介紹了Android4.0平板開發(fā)之隱藏底部任務(wù)欄的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android隱藏于顯示底部任務(wù)欄的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11Kotlin基礎(chǔ)學(xué)習(xí)之位運(yùn)算
一提起位運(yùn)算,人們往往想到它的高效性,無論是嵌入式編程還是優(yōu)化系統(tǒng)的核心代碼,適當(dāng)?shù)倪\(yùn)用位運(yùn)算總是一種迷人的手段,下面這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之位運(yùn)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11