ImageSwitcher 图片切换的控件
package com.zhu.testandroid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MyImageSwitcher extends Activity {
private ImageSwitcher imageSwitcher;
private Button btnPrevious;
private Button btnNext;
private int foot = 0;
private int[] imgRes = new int[] { R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d, R.drawable.e, };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.activity_image_sw); // 设置要使用的布局管理器
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
btnPrevious = (Button) findViewById(R.id.btnPrevious);
btnNext = (Button) findViewById(R.id.btnNext);
imageSwitcher.setFactory(new ViewFactory() {// 设置转化工厂
@Override
public View makeView() {
ImageView imageView = new ImageView(
MyImageSwitcher.this);
imageView.setBackgroundColor(0xFFFFFFFF);
imageView.setScaleType(ImageView.ScaleType.CENTER);// 居中显示
imageView
.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));// 定义组件
return imageView;
}
});
imageSwitcher.setImageResource(imgRes[foot++]);// 初始化时显示,必须放在工厂后面,否则会报NullPointerException
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));// 设置动画
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));// 设置动画
btnPrevious.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MyImageSwitcher.this.imageSwitcher
.setImageResource(imgRes[foot--]);
MyImageSwitcher.this.checkBtnEnable();
}
});
btnNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MyImageSwitcher.this.imageSwitcher
.setImageResource(imgRes[foot++]);
MyImageSwitcher.this.checkBtnEnable();
}
});
}
protected void checkBtnEnable() {// 判断按钮可用状态
if (this.foot < this.imgRes.length - 1) {
this.btnNext.setEnabled(true);
} else {
this.btnNext.setEnabled(false);
}
if (this.foot == 0) {
this.btnPrevious.setEnabled(false);
} else {
this.btnPrevious.setEnabled(true);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:text="上一张" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="true"
android:text="下一张" />
</LinearLayout>
</LinearLayout>
给工作留一个脚印,自己写一点工作必须,方便下次查询

浙公网安备 33010602011771号