---
title: "Android 切换图片ImageSwitcher"
author: "80demon"
date: "2011-11-12T00:30:00Z"
tags: []
slug: "android-imageswitcher"
draft: false
lastmod: "2011-11-18T14:38:45Z"
id: 1265747
url: "https://80demon.iteye.com/blog/1265747"
---

我们在Windows 平台上要查看多张图片，最简单的办法就是通过 "Window 图片和传真查看器“在 ”下一张“ 和”上一张“之间切换，Android平台上可以通过 ImageSwitcher 类来实现这一效果。ImageSwitcher 类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开来，因此需要实现ViewSwitcher.ViewFactory接口，通过makeView()方法来显示图片，这里会返回一个ImageView 对象，而方法 setImageResource用来指定图片资源。首先先让我们看看这个例子的运行效果。

**Java代码：**  
package eoe.Demo;

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.ImageSwitcher;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.ViewSwitcher.ViewFactory;

public class Activity01 extends Activity implements OnClickListener,  
ViewFactory {  
// 所有要显示的图片资源索引  
private static final Integer\[] imagelist = { R.drawable.img1,  
R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,  
R.drawable.img6, R.drawable.img7, R.drawable.img8, };

// 创建ImageSwitcher对象  
private ImageSwitcher m\_Switcher;  
// 索引  
private static int index = 0;

// "下一页"按钮ID  
private static final int BUTTON\_DWON\_ID = 0x123456;  
// “上一页”按钮ID  
private static final int BUTTON\_UP\_ID = 0x123457;  
// ImageSwitcher对象的ID  
private static final int SWITCHER\_ID = 0x123458;

@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
// 创建一个线性布局LinearLayout  
LinearLayout main\_view = new LinearLayout(this);  
// 创建ImageSwitcher对象  
m\_Switcher = new ImageSwitcher(this);  
// 在线性布局中添加ImageSwitcher视图  
main\_view.addView(m\_Switcher);  
// 设置ImageSwitcher对象的ID  
m\_Switcher.setId(SWITCHER\_ID);  
// 设置ImageSwitcher对象的数据源  
m\_Switcher.setFactory((ViewFactory) this);  
m\_Switcher.setImageResource(imagelist\[index]);

// 设置显示上面的线性布局  
setContentView(main\_view);  
// 设置背景图片  
main\_view.setBackgroundResource(R.drawable.xh);

// 创建“下一张”按钮  
Button next = new Button(this);  
next.setId(BUTTON\_DWON\_ID);  
next.setText("下一张");  
next.setOnClickListener((OnClickListener) this);  
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100,  
100);  
main\_view.addView(next, param);

// 创建“上一张”按钮  
Button pre = new Button(this);  
pre.setId(BUTTON\_UP\_ID);  
pre.setText("上一张");  
pre.setOnClickListener((OnClickListener) this);  
main\_view.addView(pre, param);  
}

// 事件监听、处理  
public void onClick(View v) {  
switch (v.getId()) {  
// 下一页  
case BUTTON\_DWON\_ID:  
index++;  
if (index &gt;= imagelist.length) {  
index = 0;  
}  
// ImageSwitcher对象资源索引  
m\_Switcher.setImageResource(imagelist\[index]);  
break;  
// 上一页  
case BUTTON\_UP\_ID:  
index--;  
if (index &lt; 0) {  
// 到最后一张  
index = imagelist.length - 1;  
}  
// ImageSwitcher对象资源索引  
m\_Switcher.setImageResource(imagelist\[index]);  
default:  
break;  
}  
}

public View makeView() {  
// 将所有图片通过ImageView来显示  
return new ImageView(this);  
}  
}