MyDemo.java
package com.jackie.simplegallery;
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.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;
public class MyDemo extends Activity {
private ImageSwitcher myImageSwitcher = null;
private Gallery gallery = null;
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter();
this.gallery = (Gallery) super.findViewById(R.id.gallery);
this.myImageSwitcher = (ImageSwitcher) super
.findViewById(R.id.myImageSwitcher);
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
this.gallery.setAdapter(simpleAdapter);
this.gallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String,Integer> map= (Map<String,Integer>)parent.getAdapter().getItem(position);
MyDemo.this.myImageSwitcher.setImageResource(map.get("img"));
}
}
private void initAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields(); // 取得全部的属性
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("pic_")) { // 我们需要的图片
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[x].getInt(R.drawable.class));
} catch (Exception e) {
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this, this.list,
R.layout.grid_layout, new String[] { "img" },
new int[] { R.id.img });
}
private class ViewFactoryImpl implements ViewFactory {
@Override
public View makeView() {
ImageView imageView = new ImageView(MyDemo.this);
imageView.setBackgroundColor(0xFFFFFFFF);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return imageView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
tools:context=".MyDemo" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:spacing="3dp"
android:background="#00FFFFFF"
android:gravity="center_vertical"
android:layout_width="match_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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
MyDemo.java
package com.jackie.simplegallery;
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.Menu;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;
public class MyDemo extends Activity {
private ImageSwitcher myImageSwitcher = null;
private Gallery gallery = null;
private SimpleAdapter simpleAdapter = null;
private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.initAdapter();
this.gallery = (Gallery) super.findViewById(R.id.gallery);
this.myImageSwitcher = (ImageSwitcher) super
.findViewById(R.id.myImageSwitcher);
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
this.gallery.setAdapter(simpleAdapter);
this.gallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class OnItemClickListenerImpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String,Integer> map= (Map<String,Integer>)parent.getAdapter().getItem(position);
MyDemo.this.myImageSwitcher.setImageResource(map.get("img"));
}
}
private void initAdapter() {
Field[] fields = R.drawable.class.getDeclaredFields(); // 取得全部的属性
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("pic_")) { // 我们需要的图片
Map<String, Integer> map = new HashMap<String, Integer>();
try {
map.put("img", fields[x].getInt(R.drawable.class));
} catch (Exception e) {
}
this.list.add(map);
}
}
this.simpleAdapter = new SimpleAdapter(this, this.list,
R.layout.grid_layout, new String[] { "img" },
new int[] { R.id.img });
}
private class ViewFactoryImpl implements ViewFactory {
@Override
public View makeView() {
ImageView imageView = new ImageView(MyDemo.this);
imageView.setBackgroundColor(0xFFFFFFFF);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return imageView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom"
tools:context=".MyDemo" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Gallery
android:id="@+id/gallery"
android:spacing="3dp"
android:background="#00FFFFFF"
android:gravity="center_vertical"
android:layout_width="match_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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>