Android ApiDemos example resolution (196): Views-& gt; TextSwitcher

Source: Internet
Author: User

This example introduces TextSwitcher usage. We have introduced ImageSwitcher usage in the previous section. Android ApiDemos example resolution (124): Views-> ImageSwitcher, ImageSwitcher, and TextViewSwitcher are both subclasses of ViewSwitcher, viewSwitcher is a subclass of ViewAnimator. ViewAnimator (a subclass of FrameLayout) provides support for animation effects during switching between views, while ViewAnimator is a subclass of FrameLayout, therefore, the child views contained in ViewAnimator are stacked together. Generally, you can View the top View.

ViewSwitcher can contain up to two subviews. Only one View is displayed at a time. It has two subclasses: TextSwitcher and ImageSwitcher. This example describes TextSwitcher. TextSwitcher can contain TextView. TextSwitcher can mainly use the animation effect during text switching.

When setText is called, TextSwitcher uses the set animation effect to display the switching between the new text string and the original text. You can use addView to manually add a View to ViewSwitcher for ViewSwitcher, or use ViewSwitcher. ViewFactory to automatically create a new View. In this example, ViewSwitcher. ViewFactory is used to create a new View for TextSwitcher. In the introduction, TabControl adopts a similar Factory method. Android ApiDemos example resolution (194): Views-> Tabs-> Content By Factory.


[Java]
Public class TextSwitcher1 extends Activity
Implements ViewSwitcher. ViewFactory,
View. OnClickListener {
 
Private TextSwitcher mSwitcher;
 
Private int mCounter = 0;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. text_switcher_1 );
 
MSwitcher = (TextSwitcher) findViewById (R. id. switcher );
MSwitcher. setFactory (this );
 
Animation in = AnimationUtils. loadAnimation (this,
Android. R. anim. fade_in );
Animation out = AnimationUtils. loadAnimation (this,
Android. R. anim. fade_out );
MSwitcher. setInAnimation (in );
MSwitcher. setOutAnimation (out );
 
Button nextButton = (Button) findViewById (R. id. next );
NextButton. setOnClickListener (this );
 
UpdateCounter ();
}
 
Public void onClick (View v ){
MCounter ++;
UpdateCounter ();
}
 
Private void updateCounter (){
MSwitcher. setText (String. valueOf (mCounter ));
}
 
Public View makeView (){
TextView t = new TextView (this );
T. setGravity (Gravity. TOP | Gravity. CENTER_HORIZONTAL );
T. setTextSize (36 );
Return t;
}
}

Public class TextSwitcher1 extends Activity
Implements ViewSwitcher. ViewFactory,
View. OnClickListener {

Private TextSwitcher mSwitcher;

Private int mCounter = 0;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );

SetContentView (R. layout. text_switcher_1 );

MSwitcher = (TextSwitcher) findViewById (R. id. switcher );
MSwitcher. setFactory (this );

Animation in = AnimationUtils. loadAnimation (this,
Android. R. anim. fade_in );
Animation out = AnimationUtils. loadAnimation (this,
Android. R. anim. fade_out );
MSwitcher. setInAnimation (in );
MSwitcher. setOutAnimation (out );

Button nextButton = (Button) findViewById (R. id. next );
NextButton. setOnClickListener (this );

UpdateCounter ();
}

Public void onClick (View v ){
MCounter ++;
UpdateCounter ();
}

Private void updateCounter (){
MSwitcher. setText (String. valueOf (mCounter ));
}

Public View makeView (){
TextView t = new TextView (this );
T. setGravity (Gravity. TOP | Gravity. CENTER_HORIZONTAL );
T. setTextSize (36 );
Return t;
}
}

 

Set the fade-in and fade-out animation effect for TextSwitcher to switch the text.

 

 

 

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