Android 0 Basics Section 55th: Imageswitcher and Textswitcher use

Source: Internet
Author: User

In the previous issue, we learned about the use of Viewanimator components and viewswitcher components, have you mastered them? This issue together to learn Viewswitcher's two subcomponents Imageswitcher and Textswitcher.

First, Imageswitcher

Imageswitcher and Imageswitcher inherit Viewswitcher, so it has the same characteristics as Viewswitcher: You can use animation effects when switching view components. Imageswitcher inherits the Viewswitcher and rewrites the Viewswitcher Shownext (), Showprevious () method, so imageswitcher is easier to use.

Use Imageswitcher as long as the following two steps.

    • Provides a viewfactory for Imageswitcher, the View component that the viewfactory generates must be ImageView.

    • When you need to switch pictures, just call Imageswitcher setimagedrawable (drawable drawable), setimageresource (int resid), and Setimageuri (URI Uri) method to change the picture.

Next, a simple example program is used to learn the use of imageswitcher.

To continue using the Widgetsample project's Advancedviewsample module, first prepare 5 images in the drawable directory and then create the Imageswitcher_ in the app/main/res/layout/directory Layout.xml file, where the following code snippet is populated:

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageswitcherAndroid:id= "@+id/switcher"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"android:inanimation= "@android: Anim/slide_in_left"android:outanimation= "@android: Anim/slide_out_right"/></Relativelayout>

The bold code in the above interface layout file defines a imageswitcher and specifies the animation effect when the picture is toggled through Android:inanimation and android:outanimation.

One of the most important aspects of using Imageswitcher is to specify a viewfactory for it, that is, to define how it displays the content, The general practice is to implement the Viewfactory interface in this class using Imageswitcher and overwrite the corresponding Makeview method. Create a new file and load the new layout file above, with the following code:;ImportAndroid.os.Bundle;;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importandroid.view.animation.AnimationUtils;ImportAndroid.widget.ImageSwitcher;ImportAndroid.widget.ImageView;ImportAndroid.widget.ViewSwitcher;/*** @ Creator Xin 鱻 * @ description Android 0 Basic primer to Master Series tutorials, welcome to the public number Shareexpert*/ Public classImageswitcheractivityextendsAppcompatactivityImplementsViewswitcher.viewfactory {PrivateImageswitcher Mimageswitcher =NULL; Private int[] Mimageids ={r.drawable.image_01, r.drawable.image_02, r.drawable.image_03, R.drawable.image_04, R.drawabl    E.IMAGE_05}; Private intMindex = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.imageswitcher_layout); //Get interface ComponentsMimageswitcher =(Imageswitcher) Findviewbyid (; //for him it specifies a viewfactory, which defines how it displays the content,//implement the Viewfactory interface and overwrite the corresponding Makeview method. Mimageswitcher.setfactory ( This); //Add an animation effectMimageswitcher.setinanimation (Animationutils.loadanimation ( This, Android.        r.anim.fade_in)); Mimageswitcher.setoutanimation (Animationutils.loadanimation ( This, Android.        R.anim.fade_out)); //monitoring events for Imageswitcher bindingsMimageswitcher.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Mindex++; if(Mindex >=mimageids.length) {Mindex= 0;            } mimageswitcher.setimageresource (Mimageids[mindex]);        }        }); Mimageswitcher.setimageresource (mimageids[0]); } @Override PublicView Makeview () {ImageView ImageView=NewImageView ( This); Imageview.setbackgroundcolor (0xff000000); //set the Fill methodImageview.setscaletype (ImageView.ScaleType.FIT_XY); Imageview.setlayoutparams (Newimageswitcher.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))        ; returnImageView; }}

Run the program and click Imageswitcher to see the image switching effect shown on the screen.

Second, Textswitcher

Textswitcher inherits the Viewswitcher, so it has the same characteristics as Viewswitcher: You can use animation effects when switching view components. Similar to Imageswitcher, the use of Textswitcher also requires setting up a viewfactory. Unlike Imageswitcher, the Makeview () method required by Textswitcher must return a TextView component.

Textswitcher is a bit similar to TextView's, and they can be used to display text content, except that the textswitcher effect is more flattering, and it can specify the animation effect when the text is toggled.

Next, a simple example program is used to learn the use of textswitcher.

Continue to use the Advancedviewsample module of the Widgetsample project to create a textwitcher_layout.xml file in the app/main/res/layout/directory populated with the following code snippet:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <!--defines a textswitcher and specifies the animation effect when the text is toggled -    <TextswitcherAndroid:id= "@+id/textswitcher"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:inanimation= "@android: Anim/slide_in_left"android:outanimation= "@android: Anim/slide_out_right" /></LinearLayout>

A textswitcher is defined in the interface layout file above and the animated effect is specified when the text is toggled.

The activity is then set viewfactory for Textswitcher, and the Textswitcher works as expected. Create a new file and load the new layout file above, with the following code:;;ImportAndroid.os.Bundle;;ImportAndroid.view.View;ImportAndroid.widget.TextSwitcher;ImportAndroid.widget.TextView;ImportAndroid.widget.ViewSwitcher;/*** @ Creator Xin 鱻 * @ description Android 0 Basic primer to Master Series tutorials, welcome to the public number Shareexpert*/ Public classTextswitcheractivityextendsappcompatactivity {PrivateTextswitcher Mtextswitcher =NULL; Privatestring[] Mcontents = {            "Hello", "HelloWorld", "Good!!!", "Textswitcher", "Will you?" "    }; Private intMindex = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.textwitcher_layout); Mtextswitcher=(Textswitcher) Findviewbyid (; Mtextswitcher.setfactory (Newviewswitcher.viewfactory () {@Override PublicView Makeview () {TextView TV=NewTextView (textswitcheractivity. This); Tv.settextsize (40);                Tv.settextcolor (Color.magenta); returnTV;        }        }); Mtextswitcher.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Mtextswitcher.settext (Mcontents[mindex++ %Mcontents.length]);        }        }); Mtextswitcher.settext (mcontents[0]); }}

The bold code above overrides the Viewfactory Makeview () method, which returns a TextView so that Textswitcher works correctly. When the program wants to toggle Textswitcher display text, call Textswitcher's SetText () method to modify the text.

To run the program, clicking Textswitcher will toggle the displayed text, and the animation will appear as shown in.

At this point, about the Imageswitcher and Textswitcher components learning is complete, if there is unclear where to suggest back to do more practice.

Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!

This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!

Past period Summary share:

Android 0 Basics Introduction 1th: Android's past life

Android 0 Basics Section 2nd: Android system Architecture and application components those things

Android 0 Basics Section 3rd: Bring you up to talk about Android development environment

Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick

Android 0 Basics 5th: Use ADT bundles to easily meet the goddess

Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess

Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey

Android 0 Basics 8th: HelloWorld, the starting point for my first trip

Android 0 Basics 9th: Android app, no code can be developed

Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio

Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project

Android 0 Basics 12th: Get familiar with the Android studio interface and start selling

Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool

Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era

Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing

Android 0 Basics Section 16th: Android User Interface Development overview

Android 0 Basics Section 17th: text box TextView

Android 0 Basics Section 18th: Input box EditText

Android 0 Basics Get started section 19th: Buttons button

Android 0 Basics 20th: check box checkbox and radio button radiobutton

Android 0 Basics Section 21st: Switch Components ToggleButton and switch

Android 0 Basics Section 22nd: Image View ImageView

Android 0 Basics Section 23rd: Image button ImageButton and zoom button Zoombutton

Android 0 Basics Section 24th: Customize view simple to use to create your own controls

Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts

Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly

Android 0 Basics section 27th: Using padding and margin correctly

Android 0 Basics Section 28th: Easy to master relativelayout relative layout

Android 0 Basics 29th: Use tablelayout table layouts

Android 0 Basics 30th: Two minutes master Framelayout frame layout

Android 0 Basics Section 31st: absolutelayout absolute Layout with less

Android 0 Basics Section 32nd: New GridLayout Grid layout

Android 0 Basics section 33rd: Android Event Handling overview

Android 0 Basics Section 34th: Listening-based event handling in Android

Android 0 Basics Section 35th: Callback-based event handling in Android

Android 0 Basics Section 36th: Handling of Android system events

Android 0 Basics 37th: First Knowledge ListView

Android 0 Basics 38th: First Knowledge Adapter

Android 0 Basics section 39th: listactivity and custom list items

Android 0 Basics Section 40th: Customizing Arrayadapter

Android 0 Basics Section 41st: Using Simpleadapter

Android 0 Basics Section 42nd: Customizing Baseadapter

Android 0 Basics section 43rd: ListView Optimization and List End-to-end use

Android 0 Basics Section 44th: ListView Data Dynamic Update

Android 0 Basic Getting Started section 45th: Grid view GridView

Android 0 Basics section 46th: List Options Box spinner

Android 0 Basic Getting Started section 47th: AutoComplete text Box Autocompletetextview

Android 0 Basics Section 48th: Collapsible list Expandablelistview

Android 0 Basics section 49th: Adapterviewflipper picture Carousel

Android 0 Basics Section 50th: StackView card Stacking

Android 0 Basics Section 51st: progress bar ProgressBar

Android 0 Basics section 52nd: Customizing ProgressBar Cool progress bar

Android 0 Basics 53rd: Drag bar Seekbar and star rating bar Ratingbar

Android 0 Basics section 54th: View switch Components Viewswitcher

Android 0 Basics Section 55th: Imageswitcher and Textswitcher use

Related Article

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: 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.