main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageSwitcher android:id="@+id/myImageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/butPrevious" android:text="上一张图片" android:enabled="false" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/butNext" android:text="下一张图片" android:enabled="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
MyImageSwitcherDemo.java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class MyImageSwitcherDemo extends Activity {
private ImageSwitcher myImageSwitcher = null; // 图片切换
private Button butPrevious = null; // 按钮组件
private Button butNext = null; // 按钮组件
private int[] imgRes = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,
R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e }; // 资源图片ID
private int foot = 0; // 资源读取位置
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); // 调用布局管理器
this.myImageSwitcher = (ImageSwitcher) super
.findViewById(R.id.myImageSwitcher); // 取得组件
this.butPrevious = (Button) super.findViewById(R.id.butPrevious); // 取得组件
this.butNext = (Button) super.findViewById(R.id.butNext) ; // 取得组件
this.myImageSwitcher.setFactory(new ViewFactoryImpl()); // 设置转换工厂
this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in)); // 设置动画
this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out)); // 设置动画
this.myImageSwitcher.setImageResource(imgRes[foot++]) ; // 设置图片
this.butNext.setOnClickListener(new OnClickListenerNext()) ;// 设置单击事件
this.butPrevious.setOnClickListener(new OnClickListenerPrevious()) ;// 设置单击事件
}
private class OnClickListenerPrevious implements OnClickListener {
@Override
public void onClick(View v) {
MyImageSwitcherDemo.this.myImageSwitcher
.setImageResource(imgRes[foot--]); // 修改显示图片
MyImageSwitcherDemo.this.checkButEnable(); // 设置按钮状态
}
}
private class OnClickListenerNext implements OnClickListener {
@Override
public void onClick(View v) {
MyImageSwitcherDemo.this.myImageSwitcher
.setImageResource(imgRes[foot++]); // 修改显示图片
MyImageSwitcherDemo.this.checkButEnable(); // 设置按钮状态
}
}
public void checkButEnable() { // 设置按钮状态
if (this.foot < this.imgRes.length - 1) {
this.butNext.setEnabled(true); // 按钮可用
} else {
this.butNext.setEnabled(false); // 按钮不可用
}
if (this.foot == 0) {
this.butPrevious.setEnabled(false); // 按钮不可用
} else {
this.butPrevious.setEnabled(true); // 按钮可用
}
}
private class ViewFactoryImpl implements ViewFactory {
@Override
public View makeView() {
ImageView img = new ImageView(MyImageSwitcherDemo.this); // 实例化图片显示
img.setBackgroundColor(0xFFFFFFFF); // 设置背景颜色
img.setScaleType(ImageView.ScaleType.CENTER); // 居中显示
img.setLayoutParams(new ImageSwitcher.LayoutParams( // 自适应图片大小
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定义组件
return img;
}
}
}