控件:Gallery --- 3.(实现图片切换)

                                           ◆使用Gallery + ImageSwitcher完成图片浏览功能

grid_layout.xml

View Code
<?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>

 

main.xml

View Code
<?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>

 

MyGalleryDemo.java

View Code
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
Map<String, Integer> map = (Map<String, Integer>) MyGalleryDemo
.this.simpleAdapter.getItem(position);
// 设置显示图片
MyGalleryDemo.this.myImageSwitcher.setImageResource(map.get("img"));
}
}

// 初始化适配器
public void initAdapter(){
Field[] fields = R.drawable.class.getDeclaredFields();
for (int x = 0; x < fields.length; x++) {
// 所有ispic_*命名的图片
if (fields[x].getName().startsWith("ispic_")){
// 定义Map
Map<String,Integer> map = new HashMap<String,Integer>() ;
try {
// 设置图片资源
map.put("img", fields[x].getInt(R.drawable.class)) ;
} catch (Exception e) {
}
// 保存Map
this.list.add(map) ;
}
}
// 实例化SimpleAdapter
this.simpleAdapter = new SimpleAdapter(
this,
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;
}
}

}

 

                 

          

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-03-04 16:50  大米稀饭  阅读(302)  评论(0编辑  收藏  举报