ImageSwitcher组件的主要功能是进行图片的切换操作实现,可以在屏幕上自由的对图片进行切换。
现实图片的切换操作,关键在于ViewFactory工厂的使用上。
在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_marginTop="10dp"
android:layout_marginLeft="65dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/butPrevious"
android:layout_marginTop="10dp"
android:layout_marginLeft="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="上一张图片"/>
<Button
android:id="@+id/butNext"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:text="下一张图片"/>
</LinearLayout>
</LinearLayout>
在MyImageSwitcherDemo.java程序中
package com.tarena.imageswitcher;
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 MyImageSwitcherDemo extends Activity {
private Button butPrevious = null;
private Button butNext = null;
private ImageSwitcher myImageSwitcher = null;
private int[] imgRes = new int[] { R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.d};
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.setImageResource(this.imgRes[this.foot++]);
this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
this.butPrevious.setOnClickListener(new OnClickListenerPrevious());
this.butNext.setOnClickListener(new OnClickListenerNext());
}
private class OnClickListenerPrevious implements OnClickListener {
public void onClick(View view) {
MyImageSwitcherDemo.this.myImageSwitcher
.setImageResource(MyImageSwitcherDemo.this.imgRes[MyImageSwitcherDemo.this.foot--]);
MyImageSwitcherDemo.this.checkButEnable() ;
}
}
private class OnClickListenerNext implements OnClickListener {
public void onClick(View view) {
MyImageSwitcherDemo.this.myImageSwitcher
.setImageResource(MyImageSwitcherDemo.this.imgRes[MyImageSwitcherDemo.this.foot++]);
MyImageSwitcherDemo.this.checkButEnable() ;
}
}
private 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 {
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;
}
}
}