main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom"> <ImageSwitcher android:id="@+id/myImageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Gallery android:id="@+id/myGallery" android:gravity="center_vertical" android:spacing="3px" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center"/> </LinearLayout>
MyGalleryDemo.java:
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.LinearLayout.LayoutParams; import android.widget.SimpleAdapter; import android.widget.ViewSwitcher.ViewFactory; public class MyGalleryDemo extends Activity { private Gallery gallery = null; // 图片浏览 private List<Map<String,Integer>> list = new ArrayList<Map<String,Integer>>() ; private SimpleAdapter simpleAdapter = null; // 适配器 private ImageSwitcher myImageSwitcher = null ; // 图片切换 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); // 调用布局文件 this.initAdapter() ; // 初始化适配器 this.gallery = (Gallery) super.findViewById(R.id.myGallery) ;// 取得组件 this.myImageSwitcher = (ImageSwitcher) super .findViewById(R.id.myImageSwitcher); // 取得组件 this.myImageSwitcher.setFactory(new ViewFactoryImpl()) ;// 设置图片工厂 this.gallery.setAdapter(this.simpleAdapter); // 设置图片集 this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ;// 设置单击事件 } private class OnItemClickListenerImpl implements OnItemClickListener { @SuppressWarnings("unchecked") @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, Integer> map = (Map<String, Integer>) MyGalleryDemo .this.simpleAdapter.getItem(position); // 取出Map MyGalleryDemo.this.myImageSwitcher .setImageResource(map.get("img")); // 设置显示图片 } } public void initAdapter(){ // 初始化适配器 Field[] fields = R.drawable.class.getDeclaredFields(); for (int x = 0; x < fields.length; x++) { if (fields[x].getName().startsWith("ispic_")){ // 所有ispic_*命名的图片 Map<String,Integer> map = new HashMap<String,Integer>() ; // 定义Map try { map.put("img", fields[x].getInt(R.drawable.class)) ; } catch (Exception e) { // 设置图片资源 } this.list.add(map) ; // 保存Map } } this.simpleAdapter = new SimpleAdapter(this, // 实例化SimpleAdapter this.list, // 要包装的数据集合 R.layout.grid_layout, // 要使用的显示模板 new String[] { "img" }, // 定义要显示的Map的Key new int[] {R.id.img }); // 与模板中的组件匹配 } private class ViewFactoryImpl implements ViewFactory { // 定义视图工厂类 @Override public View makeView() { ImageView img = new ImageView(MyGalleryDemo.this); // 实例化图片显示 img.setBackgroundColor(0xFFFFFFFF); // 设置背景颜色 img.setScaleType(ImageView.ScaleType.CENTER); // 居中显示 img.setLayoutParams(new ImageSwitcher.LayoutParams( // 自适应图片大小 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));// 定义组件 return img; } } }