kotlin使用android自带的TextSwitcher实现Textview的上下滚动

Elysian
2021-11-04 / 0 评论 / 393 阅读 / 正在检测是否收录...

先看效果:
20190314151405165.gif
布局:
activi

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <View
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#ccc"/>

    <TextSwitcher
        android:id="@+id/text_switcher"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#ccc"/>

</androidx.constraintlayout.widget.ConstraintLayout>

代码:MainActivity.kt

package com.chinabm.android_demo

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private val list: Array<String> = arrayOf(
        "这是第一条数据",
        "这是第二条数据",
        "这是第三条数据"
    )
    private var mHandler: Handler? = null;
    private val UPDATE_TEXT = 0;
    private var task: TimerTask? = null;
    private var timer: Timer? = null;
    private var index = 0;


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView();

        mHandler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
                if (msg.what == UPDATE_TEXT) {
                    text_switcher.setText(list.get(index));
                }
            }
        }

        task = object : TimerTask() {
            override fun run() {
                index = ++index % list.size;

                mHandler?.sendEmptyMessage(UPDATE_TEXT);
            }
        };
        timer = Timer();

        if (task != null && timer != null) {
            timer?.schedule(task, Date(), 6000)
        }

    }


    private fun initView() {
        text_switcher.setFactory(ViewSwitcher.ViewFactory(function = {
            var textView: TextView = TextView(this);
            textView.setLayoutParams(
                FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                )
            );
            textView.setGravity(Gravity.LEFT);
            textView.setSingleLine();
            textView.ellipsize = TextUtils.TruncateAt.END;
            textView.setTextSize(16F);
            textView.setTextColor(Color.WHITE);

            textView
        }));
        text_switcher.setText(list.get(0));
        text_switcher.setInAnimation(this, R.anim.show);
        text_switcher.setOutAnimation(this, R.anim.hide);

    }

}

show动画:
show.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true">

    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0%"
        android:toYDelta="0%">
    </translate>

    <alpha
        android:duration="900"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>

</set>

hide动画:
hide.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true">

    <translate
        android:duration="1000"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0%"
        android:toYDelta="-100%">
    </translate>

    <alpha
        android:duration="900"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>

</set>

两个动画文件需要放创建一个anim文件夹存放在res目录
该文使用了kotlinx插件,需要了解的可以移步 《使用kotlinx书写android实践及常见问题处理》

0

评论

博主关闭了所有页面的评论