[Android] TextSwitcher, textswitcher
In the above section, we describe how to useTextSwitcherControl. This article analyzes the source code of the Android Framework layer to explain how it implements smooth text switching.
Class inheritance relationship of TextSwitcher
With this inheritance structure, we can know that TextSwitcher:
-Inherited fromFrameLayoutSo its sub-views are stacked
-Inherited fromViewAnimator, So it holds twoAnimationObject, used to display fade-out, fade-in, and other animation effects.
setFactoryWhat have you done?
ReadViewSwitcherSource code, we can find insetFactoryWill construct2InmakeViewThe view generated in the callback.
Public void setFactory (ViewFactory factory) {mFactory = factory; // construct a sub-View obtainView (); // create a sub-View obtainView ();}
Then, add the two View objectsFrameLayout.
setInAnimationAnd
setOutAnimationWhat have you done?
Is to setViewAnimatorHoldAnimationObject.
android.view.animation.Animation
public void setInAnimation(Animation inAnimation) { mInAnimation = inAnimation; }
setTextWhat have you done?
Public void setText (CharSequence text) {final TextView t = (TextView) getNextView (); t. setText (text); // display the next View showNext ();}
Finally calledandroid.widget.ViewAnimatorOfshowOnlyMethod.
Void showOnly (int childIndex, boolean animate) {final int count = getChildCount (); for (int I = 0; I <count; I ++) {final View child = getChildAt (I); if (I = childIndex) {if (animate & mInAnimation! = Null) {// Let the new TextView demo enter the animation child. startAnimation (mInAnimation);} child. setVisibility (View. VISIBLE); mFirstTime = false;} else {if (animate & mOutAnimation! = Null & child. getVisibility () = View. VISIBLE) {// Let the old TextView demo fade out animation child. startAnimation (mOutAnimation);} else if (child. getAnimation () = mInAnimation) child. clearAnimation (); child. setVisibility (View. GONE );}}}
In this way, the text view is smoothly switched.
Note:
1. Related code is available on GitHub
2. Supporting videos in Youku