Mainactivity.java
Copy Code code as follows:
Package Com.zhang.showPhoto;
Import Android.app.ActionBar.LayoutParams;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
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 int[] Imagid=new int[]{
R.DRAWABLE.IMG01,R.DRAWABLE.IMG02,R.DRAWABLE.IMG03,R.DRAWABLE.IMG04,
R.DRAWABLE.IMG05,R.DRAWABLE.IMG06,R.DRAWABLE.IMG07,R.DRAWABLE.IMG08,
R.drawable.img09,r.drawable.img10
};
private int index=0;
Private Imageswitcher Imageswitcher;
Private Button Up,down;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
up= (Button) Findviewbyid (R.ID.BT1);
Down= (Button) Findviewbyid (R.ID.BT2);
Imageswitcher= (Imageswitcher) Findviewbyid (R.ID.IMAGSW1);
Imageswitcher.setinanimation (This, Android. Animationutils.loadanimation. r.anim.fade_in));
Imageswitcher.setoutanimation (This, Android. Animationutils.loadanimation. R.anim.fade_out));
Imageswitcher.setfactory (New Viewfactory () {
Public View Makeview () {
ImageView ImageView = new ImageView (mainactivity.this);
Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
Imageview.setlayoutparams (New Imageswitcher.layoutparams (
Layoutparams.wrap_content,layoutparams.wrap_content
));
return ImageView;
}
});
Imageswitcher.setimageresource (Imagid[index]);
Up.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
if (index>0) {
index--;
}else{
Index=imagid.length-1;
}
Imageswitcher.setimageresource (Imagid[index]);
}
});
Down.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
if (index<imagid.length-1) {
index++;
}else{
index=0;
}
Imageswitcher.setimageresource (Imagid[index]);
}
});
}
}
Main.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:background= "@drawable/bg1"
Android:id= "@+id/llayout"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:gravity= "Center"
android:orientation= "Horizontal" >
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "The Last One"
Android:id= "@+id/bt1"
/>
<imageswitcher
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:id= "@+id/imagsw1"
android:layout_gravity= "Center"
/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "The next One"
Android:id= "@+id/bt2"
/>
</LinearLayout>