activity_main.xml
控件的线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/butPrevious" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false" android:text="上一张图片"/> <Button android:id="@+id/butNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一张图片"/> </LinearLayout> </LinearLayout>
MainActivity.java
package com.example.imageswitcherproject; import android.os.Bundle; import android.app.Activity; import android.view.Menu; 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 MainActivity extends Activity { private Button butPrevious=null;//上一张图片按钮 private Button butNext=null;//下一张图片按钮 private ImageSwitcher imageSwitcher=null; private int[] imgRes={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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.butPrevious=(Button)super.findViewById(R.id.butPrevious); this.butNext=(Button)super.findViewById(R.id.butNext); this.imageSwitcher=(ImageSwitcher)super.findViewById(R.id.imageSwitcher); this.imageSwitcher.setFactory(new OnFactorylmpl());//设置转换工厂 this.imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//设置动画 this.imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//设置动画 this.imageSwitcher.setImageResource(this.imgRes[foot++]);//设置初始图片 this.butPrevious.setOnClickListener(new OnClickListenerPrevious()); this.butNext.setOnClickListener(new OnClickListenerNext()); } 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 OnClickListenerNext implements OnClickListener{//监听按钮 @Override public void onClick(View v) { MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]); MainActivity.this.checkButEnable(); } } private class OnClickListenerPrevious implements OnClickListener{//监听按钮 @Override public void onClick(View v) { MainActivity.this.imageSwitcher.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]); MainActivity.this.checkButEnable(); } } private class OnFactorylmpl implements ViewFactory{ @Override public View makeView() { ImageView image=new ImageView(MainActivity.this);//创建图片控件 image.setBackgroundColor(0xFFFFFFFF);//设置背景颜色 image.setScaleType(ImageView.ScaleType.CENTER); //设置显示方式 image.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));//定义组建 return image; } } @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; } }