//程序的实现代码
package com.example.imageswitcher;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private ImageSwitcher imageSwitcher;
private int index = 0;
private int[] image = new int[] { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.image5 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);// 无标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// 得到imageswitcher对象
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);
// 实现并设置工厂内部接口的makeView方法,用来显示视图
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(MainActivity.this);
}
});
// 图片来源
imageSwitcher.setImageResource(image[index]);
// 点击事件
imageSwitcher.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
index++;
if (index >= image.length) {
index = 0;
}
imageSwitcher.setImageResource(image[index]);
}
});
// 动画切入效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left));
// 动画切出效果
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//程序的布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" />
</RelativeLayout>