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.