//消息
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}")
}
}
}
网友评论