Simple use of ImageSwitcher,

Source: Internet
Author: User

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 );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.