Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库
方法原型: public void setSelection (int position)
========================
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; 、//线程循环运行的条件
private ImageSwitcher imgSwitcher;
private Gallery mgallery;
// 为Gallery控件设置Adapter
private ImageAdapter imgAdapter = null;
/** Called when the activity is first created. */
@Override
public 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); //设置图片之间的间隔,如果不加设置 ,图片会叠加。设置为0,表示图片之间无间缝。
// 设置监听事件 --->当Gallery中的Item处于选中并且被点击触发该事件
mgallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
System.out.println("setOnItemClickListener");
// imgSwitcher.setBackgroundResource(imgAdapter.getResId(position));
}
});
//当Gallery中的Item处于选中并且被点击触发该事件 ,在该监听事件中,保证图片播放的同步性
mgallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
System.out.println("setOnItemSelectedListener");
//这儿不能通过setImageResource()设置图片
imgSwitcher.setBackgroundResource(imgAdapter.getResId(position));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//构建适配器,并且分配
imgAdapter = new ImageAdapter(this);
mgallery.setAdapter(imgAdapter);
count_drawble = imgAdapter.getCount();
// 利用线程来更新 当前欲显示的图片id, 调用handler来选中当前图片
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isalive) {
cur_index = cur_index % count_drawble; // 图片区间[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处理消息的同步性,导致cur_index>=count_drawble
}
}
}).start();
}
//通过handler来更新主界面 mgallery.setSelection(positon),选中第position的图片,然后调用OnItemSelectedListener监听改变图像
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监听更改
//imgSwitcher.setBackgroundResource(imgAdapter.getResId(msg.arg1));
}
}
};
public void onDestroy() {
super.onDestroy();
isalive = false;
}
@Override
public View makeView() { //ImageSwitcher的ViewFactory方法
// TODO Auto-generated method stub
ImageView img = new ImageView(this);
return img;
}
// 为Gallery控件提供适配器的类
class ImageAdapter extends BaseAdapter {
private Context mcontext;
private ArrayList<Integer> residList = new ArrayList<Integer>(); // 通过放射机制保存所有图片的id
public ImageAdapter(Context context) {
mcontext = context;
// 反射的可重用性更好
// R.id在R文件中本质上是一个类,我们通过这个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() = 0x7f020000
if (!"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();
}
}
}
}
@Override
public int getCount() {
Log.e(TAG, " " + residList.size());
return residList.size();
}
@Override
public Object getItem(int position) {
return residList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
//得到该图片的res id
public int getResId(int position) {
return residList.get(position);
}
@Override
public 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;
}
}
}