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"
android:gravity="center_horizontal">
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ImageSwitcher>
<TextSwitcher
android:id="@+id/textSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一张" >
</Button>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一张" >
</Button>
</LinearLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ImageTextSwitcher</string>
<string name="photo_1">图片1</string>
<string name="photo_2">图片2</string>
<string name="photo_3">图片3</string>
<string name="photo_4">图片4</string>
<string name="photo_5">图片5</string>
<string name="photo_6">图片6</string>
<string name="photo_7">图片7</string>
</resources>
ActivityMain
package com.android;
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.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class ActivityMain extends Activity {
private ImageSwitcher mImageSwitcher;
private TextSwitcher mTextSwitcher;
//当前显示的索引
private int current;
//图片资源
private Integer[] mImageIds = {
R.drawable.photo_1, R.drawable.photo_2, R.drawable.photo_3,
R.drawable.photo_4, R.drawable.photo_5, R.drawable.photo_6,
R.drawable.photo_7};
//文本资源
private Integer[] mTextIds = {
R.string.photo_1, R.string.photo_2, R.string.photo_3,
R.string.photo_4, R.string.photo_5, R.string.photo_6,
R.string.photo_7};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取ImageSwitcher对象
mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
//设置ViewFactory
mImageSwitcher.setFactory(new ViewFactory (){
@Override
public View makeView() {
//ImageSwitcher需要返回ImageView对象
ImageView i = new ImageView(ActivityMain.this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}
});
//设置动画淡然淡出效果
mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
//默认值
mImageSwitcher.setImageResource(mImageIds[0]);
current = 0;
//TextSwitcher例子
mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
mTextSwitcher.setFactory(new ViewFactory (){
@Override
public View makeView() {
// TODO Auto-generated method stub
//ImageSwitcher需要返回ImageView对象
//TextSwitcher需要返回TextView对象
TextView i = new TextView(ActivityMain.this);
i.setTextSize(25);
return i;
}
});
mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
mTextSwitcher.setText(this.getResources().getText(mTextIds[0]));
//按钮定义
Button previousBtn = (Button) findViewById(R.id.previous);
Button nextBtn = (Button) findViewById(R.id.next);
previousBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(current>0){
current--;
//设置ImageSwitcher要显示的图片
mImageSwitcher.setImageResource(mImageIds[current]);
mTextSwitcher.setText(ActivityMain.this.getResources().getText(mTextIds[current]));
}
}
});
nextBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(current<mImageIds.length-1){
current++;
mImageSwitcher.setImageResource(mImageIds[current]);
mTextSwitcher.setText(ActivityMain.this.getResources().getText(mTextIds[current]));
}
}
});
}
}