Simple tutorial for Android-gun 8 (ImageSwitcher usage example ),
The main function of the ImageSwitcher component is to display images. For example, when you browse images, you can click the button to switch the displayed images one by one. You can also add some animation effects during the switchover.
If you want to implement the image switching function, the defined Activity class must also implement the ViewSwitcher. ViewFactory interface to specify the operation factory for switching views. This interface is defined as follows:
Android. widget
Interface ViewSwitcher. ViewFactory
-
Inclusiveness:
-
ViewSwitcher
-
public static interface ViewSwitcher.ViewFactory
Creates views in a ViewSwitcher.
Method Summary |
View |
makeView() Creates a new View to be added in a ViewSwitcher. |
MakeView
View makeView()
-
Creates a new View to be added in aViewSwitcher.
-
-
Return Value:
-
A View
Only one makeView () method exists in this interface. The main function of this method is to return multiple stem setting parameters of a View object.
Let's take a look at the code below:
1. main. xml code:
<? 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>
2. The MainActivity. java code is as follows:
Package org. yayun. demo; import android. r. anim; import android. r. integer; 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. animation; 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. ispic_a, R. drawable. ispic_ B, R. drawable. ispic_c, R. drawable. ispic_d, R. drawable. ispic_e ,}; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method super. setContentView (R. layout. main); // set 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 public View makeView () {// override method ImageView imageView = new ImageView (MainActivity. this); imageView. setBackgroundColor (0 xFFFFFFFF); imageView. setScaleType (ImageView. scaleType. CENTER); // CENTER the imageView. setLayoutParams (new ImageSwitcher. layoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT); // defines the component return imageView;}); imageSwitcher. setImageResource (imgRes [foot ++]); // It is displayed at initialization and must be placed behind the factory. Otherwise, NullPointerExceptionimageSwitcher will be 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 );}}}
3. Run the following instance:
Summary
1. If the setFactory () method is not set, the NullPointerException error will occur.
2. How to set the animation:SetInAnimation(AnimationUtils. loadAnimation (this, android. R. anim. fade_in ));
3. Check whether the Custom button is available using checkBtnEnable ()