Android Gallery和ImageSwitcher同步自动(滚动)播放图片库
本文主要內(nèi)容是如何讓Gallery和ImageSwitcher控件能夠同步自動播放圖片集 ,看起來較難,然而,實(shí)現(xiàn)的方法非常簡單,
請跟我慢慢來。總的來說,本文要實(shí)現(xiàn)的效果如下圖:(截圖效果不怎么好)
????????????????
???? ?本文是建立在以下兩篇bolg上的:
?????????????????1、Android入門第十二篇之Gallery
??????????????? ?2、Android 控件之ImageSwitcher圖片切換器
?????? 如果對Gallery和ImageSwitcher控件不是很熟悉的同學(xué),建議先過去看看,本文并沒有怎么講述控件的使用方法,而是在使用
基礎(chǔ)上,搭建我們的技巧。
???????接下來,溫習(xí)鞏固這兩個控件的知識點(diǎn),有個知識性的儲備。
?一、? Gallery的監(jiān)聽事件
? ??????? ?Gallery的兩個重要監(jiān)聽事件如下:???
??????????????? 1、OnItemClickListener?監(jiān)聽事件
????????? ?????????? 說明:當(dāng)Gallery中的Item處于選中狀態(tài)并且被點(diǎn)擊觸發(fā)該事件 ?;
???????????????????? 其監(jiān)聽方法為:
???????????????????????public voidonItemClick(AdapterView<?> parent, View view, int position, long id)
?????????????? 2、OnItemSelectedListener??監(jiān)聽事件?
? ?????????????????? 說明:當(dāng)Gallery中的Item處于選中狀態(tài)時觸發(fā)該事件
????????????????????? 其監(jiān)聽方法為:
?????????????????????? ?public voidonItemSelected(AdapterView<?> parent, View view, int position, long id)
? ??????????????????????? ? 說明:當(dāng)Gallery中的Item處于選中狀態(tài)時觸發(fā)該事件
??????????????????????? public void ?onNothingSelected(AdapterView<?> parent)
?????????????????????????? 說明:當(dāng)控件沒有任何一項(xiàng)item選中時,觸發(fā)該方法
???????????兩種監(jiān)聽事件的區(qū)別在于,Item被選中(selected)的由來。其由來有兩種:
???????????????????? 1、鼠標(biāo)點(diǎn)擊(click)了Item (先click),然后該項(xiàng)selected ;
???????????????????? 2、代碼設(shè)置某項(xiàng)Item 選中,例如setSelection(int position)(具體使用見下文) ,然后該項(xiàng)selected .
?????在情形1時,首先觸發(fā)OnItemClickListener(先click),接著便是OnItemSelectedListener監(jiān)聽(因?yàn)閕tem selected)。當(dāng)某個Item
??處于選中狀態(tài)時,如果它是由情形2而來,就不會觸發(fā)OnItemClickListener監(jiān)聽(沒有click),只會觸發(fā)OnItemSelectedListener監(jiān)聽
(只是selected)。
?二、Gallery的setSelection()方法
?????????方法原型:?public void setSelection (int position)
???????????????? 說明:使第position處于選中狀態(tài)。同時觸發(fā)OnItemSelectedListener監(jiān)聽事件。
???????PS: 在listView控件中也存在setSelection()是讓該行處于屏幕可見狀態(tài),不需要手動滑動定位。第一次進(jìn)入界面時,
?? 默認(rèn)顯示第一行,于是乎,我們可以手動設(shè)置該方法,ListView在顯示時,便可主動定位該item了。
?準(zhǔn)備材料已經(jīng)上齊,相信通過前面的介紹,您一定對Gallery和ImageSwitcher控件很熟悉了,下面準(zhǔn)備大餐!
????????? 步驟一:開啟一個線程,循環(huán)遍歷圖片集的資源id,并且將id發(fā)送至Hanlder對象。
??????????步驟二:Handler接受到當(dāng)前圖片資源的ID,調(diào)用setSelection (id)選中它(該Item selected),繼而setSelection()
?????????????? 觸發(fā)OnItemSelectedListener 事件,執(zhí)行目標(biāo)方法,這樣我們的目的就達(dá)到了。
? 下面,給出該Demo,希望能幫助大家更好的理解。
? ?1 、 布局文件 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"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /><ImageSwitcher android:id="@+id/myimgSwitcher" android:layout_gravity="center"android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="20dip"></ImageSwitcher><Gallery android:id="@+id/mygallery" android:layout_width="fill_parent"android:layout_height="wrap_content" android:background="#8A2BE2"></Gallery> </LinearLayout>? 2 、主文件 MainActivity.Java
package com.lover.qinjun;import java.lang.reflect.Field; import java.util.ArrayList;import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements ViewFactory {private static String TAG = "Gallery_Auto";private static int MSG_UPDATE = 1;private int count_drawble = 0;private int cur_index = 0;private boolean isalive = true; 、//線程循環(huán)運(yùn)行的條件 private ImageSwitcher imgSwitcher;private Gallery mgallery;// 為Gallery控件設(shè)置Adapterprivate ImageAdapter imgAdapter = null;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);imgSwitcher = (ImageSwitcher) findViewById(R.id.myimgSwitcher);mgallery = (Gallery) findViewById(R.id.mygallery);mgallery.setSpacing(3); //設(shè)置圖片之間的間隔,如果不加設(shè)置 ,圖片會疊加。設(shè)置為0,表示圖片之間無間縫。// 設(shè)置監(jiān)聽事件 --->當(dāng)Gallery中的Item處于選中并且被點(diǎn)擊觸發(fā)該事件 mgallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View view,int position, long arg3) {System.out.println("setOnItemClickListener"); // imgSwitcher.setBackgroundResource(imgAdapter.getResId(position)); }});//當(dāng)Gallery中的Item處于選中并且被點(diǎn)擊觸發(fā)該事件 ,在該監(jiān)聽事件中,保證圖片播放的同步性mgallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View view,int position, long arg3) {System.out.println("setOnItemSelectedListener");//這兒不能通過setImageResource()設(shè)置圖片 imgSwitcher.setBackgroundResource(imgAdapter.getResId(position));}@Overridepublic void onNothingSelected(AdapterView<?> arg0) {}});//構(gòu)建適配器,并且分配imgAdapter = new ImageAdapter(this);mgallery.setAdapter(imgAdapter);count_drawble = imgAdapter.getCount();// 利用線程來更新 當(dāng)前欲顯示的圖片id, 調(diào)用handler來選中當(dāng)前圖片new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubwhile (isalive) {cur_index = cur_index % count_drawble; // 圖片區(qū)間[0,count_drawable)Log.i(TAG, "cur_index"+ cur_index +" count_drawble --"+ count_drawble);//msg.arg1 = cur_index Message msg = mhandler.obtainMessage(MSG_UPDATE, cur_index, 0);mhandler.sendMessage(msg);//更新時間間隔為 2s try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}cur_index++; //放置在Thread.sleep(2000) ;防止mhandler處理消息的同步性,導(dǎo)致cur_index>=count_drawble }}}).start();}//通過handler來更新主界面 mgallery.setSelection(positon),選中第position的圖片,然后調(diào)用OnItemSelectedListener監(jiān)聽改變圖像private Handler mhandler = new Handler() {public void handleMessage(Message msg) {if (msg.what == MSG_UPDATE) {Log.i(TAG, "cur_index"+ cur_index);mgallery.setSelection(msg.arg1);//UI Thread直接更改圖片 ,不利用Gallery.OnItemSelectedListener監(jiān)聽更改//imgSwitcher.setBackgroundResource(imgAdapter.getResId(msg.arg1)); }}};public void onDestroy() {super.onDestroy();isalive = false;}@Overridepublic View makeView() { //ImageSwitcher的ViewFactory方法// TODO Auto-generated method stubImageView img = new ImageView(this);return img;}// 為Gallery控件提供適配器的類class ImageAdapter extends BaseAdapter {private Context mcontext;private ArrayList<Integer> residList = new ArrayList<Integer>(); // 通過放射機(jī)制保存所有圖片的idpublic ImageAdapter(Context context) {mcontext = context;// 反射的可重用性更好// R.id在R文件中本質(zhì)上是一個類,我們通過這個R.id.class.getClass().getDeclaredFields()可以找到它的所有屬性Field[] residFields = R.drawable.class.getDeclaredFields();for (Field residField : residFields) {// 例如: public static final int icon=0x7f020000;// 它的Field表示為 : name= icon ; field.getInt() = 0x7f020000if (!"icon".equals(residField.getName())) {int resid;try {resid = residField.getInt(null);// 找到該屬性的值 residList.add(resid);} catch (IllegalArgumentException e) {// TODO Auto-generated catch block e.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch block e.printStackTrace();}}}}@Overridepublic int getCount() {Log.e(TAG, " " + residList.size());return residList.size();}@Overridepublic Object getItem(int position) {return residList.get(position);}@Overridepublic long getItemId(int position) {return 0;}//得到該圖片的res idpublic int getResId(int position) {return residList.get(position);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView img;if (convertView == null) {img = new ImageView(mcontext);img.setScaleType(ImageView.ScaleType.FIT_XY);img.setLayoutParams(new Gallery.LayoutParams(100, 100)); // 圖片顯示寬和長img.setImageResource(residList.get(position));} else {img = (ImageView) convertView;}return img;}} }?主要的工程邏輯如上 ,由于采用了放射機(jī)制,直接添加圖片資源便可成功運(yùn)行了。
???????本文源代碼地址為:http://download.csdn.net/detail/qinjuning/3888134
總結(jié)
以上是生活随笔為你收集整理的Android Gallery和ImageSwitcher同步自动(滚动)播放图片库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Log保存文件-Android
- 下一篇: 基于微服务API级权限的技术架构
