TextSwitcher笔记

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

从PreferenceScreen里启动Activity

<PreferenceScreen
    android:summary="通過intent啓動Activity"
    android:title="intent偏好">

    <intent
        android:action="android.intent.action.MAIN"
        android:targetPackage="com.example.zxz.asdemo"
        android:targetClass="com.example.zxz.asdemo.ui.TextSwitcherActivity"/>
</PreferenceScreen>

<activity android:name=".ui.TextSwitcherActivity">
    <action android:name="android.intent.action.MAIN" />
</activity>

布局layout_text_switcher.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextSwitcher
        android:id="@+id/text_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/change_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


动画res/anim/新建动画资源in和out

anim_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>

anim_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>


Activity.java

public class TextSwitcherActivity extends Activity implements View.OnClickListener{
    TextSwitcher mSwitcher;
    int index = 0;
    String[] mArray = {"We", "are", "good", "at", "learning","abcdefghijklmnopqrstuvwxyzadcefghij"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_text_switcher);
        findViewById(R.id.change_btn).setOnClickListener(this);
        mSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.anim_out_left);
        Animation in = AnimationUtils.loadAnimation(this, R.anim.anim_in_right);
        mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView view = new TextView(TextSwitcherActivity.this);
                view.setSingleLine();
                view.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                view.setGravity(Gravity.CENTER);
                view.setTextSize(36);
                view.setTextColor(Color.BLUE);
                return view;
            }
        });
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);
        mSwitcher.setText(mArray[index]);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_btn:
                next();
                break;
            default:
                break;
        }
    }

    private void next() {
        mSwitcher.setText(mArray[++index % mArray.length]);
    }
}



在xml中

android:ellipsize = "end"   省略号在结尾

android:ellipsize = "start"   省略号在开头

android:ellipsize = "middle" 省略号在中间

android:ellipsize = "marquee" 跑马灯

最好加一个约束android:singleline = "true" 或者 android:maxLines="1"


当然也可以用代码语句

tv.setEllipsize(TextUtils.TruncateAt.valueOf("END"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("START"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE"));

tv.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE"));

最好再加一个约束tv.setSingleLine(true);


注:EditText中不支持marquee模式!


你可能感兴趣的:(TextSwitcher笔记)