This article describes how to use the ImageSwitcher control in AndroidUI development. The ImageSwitcher control is similar to the ImageView control, which can be used to display images. However, the ImageSwitcher name can be used to display multiple images. This article describes how to set basic attributes of the ImageSwitcher control and call common methods. At last, we will use an example Demo to show some of the content mentioned in this blog.
ImageSwitcher
ImageSwitcher is an image switcher that indirectly inherits from the FrameLayout class. Compared with ImageView, ImageSwitcher has an additional function, that is, when switching the displayed image, you can set the animation effect, it is similar to the fade-in and fade-out effect, and the left-in and right-out sliding effect.
Since ImageSwitcher is a widget used to display images, AndroidAPI provides three methods for us to set different image sources:
SetImageDrawable (Drawable): Specifies a Drawable object for display to ImageSwitcher.
SetImageResource (int): Specifies the ID of a resource to be displayed to ImageSwitcher.
SetImageURL: Specifies a URL to show ImageSwitcher the image resource that the URL points.
Animation effect settings
As described above, ImageSwitcher can set the animation effect during image switching. The support for animation effects is because it inherits the ViewAnimator class, which defines two attributes to determine the animation effect of the cut-in image and the animation effect of the cut-out image:
Android: inAnimation: the effect of image cutting.
Android: outAnimation: the effect of cropping an image.
If the preceding two attributes are set in XML, you can use the XML resource file to customize the animation effect. However, if you only want to use some simple effects provided by Android, you need to set the parameter "@ android: anim/AnimName" to set the effect, where AnimName is the specified animation effect. If it is set in the code, you can directly use the setInAnimation () and setOutAnimation () methods. They all pass an Animation abstract object. Animation is used to describe an Animation effect. It is generally obtained using an AnimationUtils tool class. Animation effects are not the focus of this blog. We will explain the animation effects of Android in detail later.
Animation effects are generally defined in android. r. in the anim class, it is a final class that defines styles in the form of int constants. Here we only introduce two sets of styles: fade-in and fade-out effect and left-in and right-out sliding effect, for other effects, see the official documentation.
Fede_in: light.
Fade_out: fade out
Slide_in_left: slide from the left.
Slide_out_right: slide from the right.
In general use, we can see through these constant names what the effect is. Here we do not force Xxx_in_Xxx to correspond to the setInAnimation () method, but if it is not set in a group, the effect is ugly. We recommend that you set the In and Out settings for groups.
ViewFactory
When using ImageSwitcher, you must use the setFactory () method to set a ViewSwitcher. viewFactory interface. When setting this ViewFactory interface, you need to implement the makeView () method. This method usually returns an ImageView, while ImageSwitcher is responsible for displaying this ImageView. If ViewFactory is not set, ImageSwitcher cannot be used. According to official documents, the setFactory () method is declared in the ViewSwitcher class, while ImageSwitcher directly inherits from the ViewSwitcher class. ViewSwitcher is similar to ImageSwitcher, but ImageSwitcher is used to display images, while ViewSwitcher is used to display some View views.
It can be understood that a new View is returned through the makeView () method in ViewFactory for display in ViewSwitcher. For ImageSwitcher, an ImageView is usually returned here.