Android TextSwitcher官方使用教程

1.TextSwitcher

TextSwitcher被用来使屏幕上的label产生动画效果。每当setText(CharSequence)被调用时,TextSwitcher使用动画方式将当前的文字内容消失并显示新的文字内容。

public void setText (CharSequence text)

设置下一视图的文本内容并切换到下一视图。可以动画的退出当前文本内容,显示下一文本内容。

参数 text 需要显示的新文本内容

2.实例Demo

一、sample_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top|center_horizontal"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/intro" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next" />

    <TextSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

二、strings.xml代码

<resources>

    <string name="intro">
    This sample illustrates the use of a <b>TextSwitcher</b> to display text.
\n\n<b>Click the button</b> below to set new text in the TextSwitcher and observe the in and out
    </string>
    <string name="next">Next</string>

</resources>

三、MainActivity.java代码

public class MainActivity extends Activity {
    private TextSwitcher mSwitcher;
    private int mCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);

        // Get the TextSwitcher view from the layout
        mSwitcher = (TextSwitcher) findViewById(R.id.switcher);


        // Set the factory used to create TextViews to switch between.
        mSwitcher.setFactory(mFactory);

        /* * Set the in and out animations. Using the fade_in/out animations * provided by the framework. */
        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);


        /* * Setup the 'next' button. The counter is incremented when clicked and * the new value is displayed in the TextSwitcher. The change of text is * automatically animated using the in/out animations set above. */
        Button nextButton = (Button) findViewById(R.id.button);
        nextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCounter++;

                mSwitcher.setText(String.valueOf(mCounter));

            }
        });

        // Set the initial text without an animation
        mSwitcher.setCurrentText(String.valueOf(mCounter));

    }


    /** * The {@link android.widget.ViewSwitcher.ViewFactory} used to create {@link android.widget.TextView}s that the * {@link android.widget.TextSwitcher} will switch between. */
    private ViewFactory mFactory = new ViewFactory() {

        @Override
        public View makeView() {

            // Create a new TextView
            TextView t = new TextView(MainActivity.this);
            t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
            t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
            return t;
        }
    };

}

四、ViewFactory 类概述

  1. 在视图转换器(ViewSwitcher)中创建视图(ImageSwitcher, TextSwitcher常实现此接口来生成视图)。

  2. public abstract View makeView ()

     创建一个用于添加到视图转换器(ViewSwitcher)中的新视图

    ————返回值 a View 一个视图

五、效果图

Android TextSwitcher官方使用教程_第1张图片

你可能感兴趣的:(android,显示,效果,TextSwitch)