[Android] TextSwitcher, textswitcher

Source: Internet
Author: User

[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

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.