1.布局文件activity.xml
<RelativeLayout 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"
>
<!-- ImageSwitcher的布局 -->
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="184dp"
>
</ImageSwitcher>
<!-- Gallery的布局 -->
<Gallery
android:id="@+id/gallery1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_horizontal"
/>
</RelativeLayout>
2.MainActivity.java
package cn.edu.cn.imageswitcher;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements OnItemSelectedListener,
ViewFactory {
private ImageSwitcher is;
private Gallery gallery;
// 画廊显示的小图
private Integer[] mThumIds = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d };
// ImageSwitcher显示的所对应的大图
private Integer[] mImageIds = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 此句话的含义为设置Activity无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 查找组件
is = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);
is.setFactory(this);
// 切入动画
is.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
// 动画显示
is.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
// 查找组件
gallery = (Gallery) this.findViewById(R.id.gallery1);
// 为Gallery设置适配器
gallery.setAdapter(new ImageAdapter(this));
// 设置监听事件
gallery.setOnItemSelectedListener(this);
}
// 适配器继承自BaseAdapter
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;;
public ImageAdapter() {
// 为图片加边框
TypedArray typedArray = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
typedArray.recycle();
}
public ImageAdapter(Context c) {
mContent = c;
}
@Override
public int getCount() {
return mThumIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContent);
i.setImageResource(mThumIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
private Context mContent;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0XFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
is.setImageResource(mImageIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
3.运行效果图