美文网首页
android TextSwitcher一个带有文字切换动画效果

android TextSwitcher一个带有文字切换动画效果

作者: hao_developer | 来源:发表于2021-09-23 15:39 被阅读0次
    652b6dfaaeed5905abdaa08971f45402.gif
    //消息
     textSwitcher.setFactory {
     val textView = TextView(activity)
     textView.textSize = 40f
     textView.setTextColor(resources.getColor(R.color.black_33))
     textView.textSize = 15f
     textView.isSingleLine = true
     textView.ellipsize = TextUtils.TruncateAt.END;
     textView
    }
    messageHandleTime()
    
     /**
        * 每间隔2秒切换message
     */
    private fun messageHandleTime(){
         messageDis = Observable.interval(0,3, TimeUnit.SECONDS)
             .subscribeOn(Schedulers.newThread())
             .subscribe {
               activity?.runOnUiThread {
                 textSwitcher.setText(messages[messageIndex ++ % messages.size]);
               }
          }
    }
    

    布局

    <TextSwitcher
       android:id="@+id/textSwitcher"
       android:inAnimation="@anim/message_in_anim"
       android:outAnimation="@anim/message_out_anim"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerVertical="true"
       android:layout_marginLeft="7dp"
      android:layout_marginRight="7dp"/>
    

    动画
    message_in_anim

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500">
    
        <translate
            android:fromXDelta="0"
            android:fromYDelta="100%"
            android:toXDelta="0"
            android:toYDelta="0" />
    
    </set>
    

    message_out_anim

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500">
    
        <translate
            android:fromXDelta="0"
            android:fromYDelta="0"
            android:toXDelta="0"
            android:toYDelta="-100%" />
    
    </set>
    

    点击事件

    textSwitcher.setOnClickListener(this)
    
    override fun onClick(v: View?) {
            when(v?.id){
                R.id.textSwitcher ->{//message点击
                    activity?.applicationContext?.toast("${messageIndex % messages.size}")
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:android TextSwitcher一个带有文字切换动画效果

          本文链接:https://www.haomeiwen.com/subject/pqrogltx.html