1.页面效果
2.页面布局的设计main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher android:layout_alignParentLeft="true"
android:id="@+id/imageSwitcher1" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:layout_alignParentTop="true"></ImageSwitcher>
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="60dip"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" android:gravity="center" android:spacing="10dip"/>
</RelativeLayout>
3.功能的实现
package cn.edu.bzu.ui;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherGalleryActivity extends Activity {
ImageSwitcher imageSwitcher = null;
Gallery gallery = null;
// 存放大图的数组
private int[] imagesLarge = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };
// 存放小图的数组
private int[] imagesSmall = { R.drawable.sample_thumb_0,
R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
R.drawable.sample_thumb_3, R.drawable.sample_thumb_4,
R.drawable.sample_thumb_5, R.drawable.sample_thumb_6,
R.drawable.sample_thumb_7 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// 得到组件
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);
gallery = (Gallery) this.findViewById(R.id.gallery);
imageSwitcher.setFactory(new MyViewFactory());
// imageSwitcher.setImageResource(imagesLarge[imagePostion]);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(imagesSmall.length/2);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
imageSwitcher.setImageResource(imagesLarge[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private class MyViewFactory implements ViewFactory {
@Override
public View makeView() {
ImageView imageView = new ImageView(
ImageSwitcherGalleryActivity.this);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
}
private class ImageAdapter extends BaseAdapter {
private Context context;
int mGalleryItemBackground;
public ImageAdapter(Context context) {
this.context = context;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imagesSmall.length;
}
@Override
public Object getItem(int position) {
return imagesSmall[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(
ImageSwitcherGalleryActivity.this);
imageView.setImageResource(imagesSmall[position]);
// imageView.setAdjustViewBounds(true);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}