Simple use of ImageSwitcher,
Test code:
Activity_main.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/MyLayout" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <ImageSwitcher android: id = "@ + id/imageSwitcher" android: layout_gravity = "center" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/btnPrevious" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: enabled = "false" android: text = "previous"/> <Button android: id = "@ + id/btnNext" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: enabled = "true" android: text = "Next"/> </LinearLayout>
MainActivity. java:
Package com. example. zz; 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 MainActivity extends Activity {private ImageSwitcher imageSwitcher; private Button btnPrevious; private Button btnNext; private int foot = 0; private int [] imgRes = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e ,}; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method super. setContentView (R. layout. activity_main); // you can specify the layout manager imageSwitcher = (ImageSwitcher) findViewById (R. id. imageSwitcher); btnPrevious = (Button) findViewById (R. id. btnPrevious); btnNext = (Button) findViewById (R. id. btnNext); imageSwitcher. setFactory (new ViewFactory () {// set the conversion factory @ Override public View makeView () {ImageView imageView = new ImageView (MainActivity. this); imageView. setBackgroundColor (0 xFFFFFFFF); imageView. setScaleType (ImageView. scaleType. CENTER); // CENTER the imageView. setLayoutParams (new ImageSwitcher. layoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); // defines the component return imageView;}); imageSwitcher. setImageResource (imgRes [foot ++]); // It is displayed at initialization and must be placed behind the factory. Otherwise, NullPointerException imageSwitcher is reported. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_in); // sets the animation imageSwitcher. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. fade_out); // sets the animation btnPrevious. setOnClickListener (new OnClickListener () {public void onClick (View v) {MainActivity. this. imageSwitcher. setImageResource (imgRes [foot --]); MainActivity. this. checkBtnEnable () ;}}); btnNext. setOnClickListener (new OnClickListener () {public void onClick (View v) {MainActivity. this. imageSwitcher. setImageResource (imgRes [foot ++]); MainActivity. this. checkBtnEnable () ;}}) ;}protected void checkBtnEnable () {// you can determine the available status of the button if (this. foot <this. imgRes. length-1) {this. btnNext. setEnabled (true);} else {this. btnNext. setEnabled (false);} if (this. foot = 0) {this. btnPrevious. setEnabled (false);} else {this. btnPrevious. setEnabled (true );}}}