效果如下圖,點擊小圖顯示大圖:
activity_image_switcher_and_gallery.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:id="@+id/imageswitch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"></ImageSwitcher>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_gravity="center"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
ImageSwitcherAndGalleryActivity.java中的代碼:
package com.bzu.imageswitcher_gallery.activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager.LayoutParams;
public class ImageSwitcherAndGalleryActivity extends Activity {
// 存放大圖片
int[] bigImage = { 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 };
// 存放小圖片
int[] smallImage = { 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);
setContentView(R.layout.activity_image_switcher_and_gallery);
final ImageSwitcher imageswitch = (ImageSwitcher) this
.findViewById(R.id.imageswitch);
Gallery gallery = (Gallery) this.findViewById(R.id.gallery);
imageswitch.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(
ImageSwitcherAndGalleryActivity.this);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
});
//final ImageAdapter myImageAdapter=new ImageAdapter(this);
gallery.setAdapter(new ImageAdapter(this));
//imageswitch.setImageResource((int) myImageAdapter.getItemId(0));
gallery.setSelection(smallImage.length/2);
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
imageswitch.setImageResource(bigImage[position]);
}
});
}
//定製適配器
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() {
return smallImage.length;
}
@Override
public Object getItem(int position) {
return smallImage[position];
}
@Override
public long getItemId(int position) {
return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(
ImageSwitcherAndGalleryActivity.this);
imageView.setImageResource(smallImage[position]);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image_switcher_and_gallery,
menu);
return true;
}
}