Android使用kotlin實(shí)現(xiàn)多行文本上下滾動播放
最近在項(xiàng)目中用到了上下滾動展示條目內(nèi)容,就使用kotlin簡單編寫實(shí)現(xiàn)了一下該功能。
使用kotlin實(shí)現(xiàn)viewflipper展示textview的上下滾動播放
其中包含了kotlin的一些簡單的使用
- 首先是在布局文件中如下代碼:
<ViewFlipper ? ? ? ? android:id="@+id/viewFlipper" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="200dp" ? ? ? ? android:background="#555" ? ? ? ? android:flipInterval="2000"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/one" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="這是靜態(tài)添加的條目1" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:textColor="#f99" /> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/two" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="這是靜態(tài)添加的條目2" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:textColor="#f99" /> </ViewFlipper>
布局中首先已經(jīng)動態(tài)添加了兩條textview文本,也可以如下面代碼中進(jìn)行動態(tài)添加textview文本
在kotlin代碼中如下:
import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.Gravity import android.view.animation.AnimationUtils import android.widget.TextView import kotlinx.android.synthetic.main.activity_flip.* class FlipActivity : AppCompatActivity() { ? ? override fun onCreate(savedInstanceState: Bundle?) { ? ? ? ? super.onCreate(savedInstanceState) ? ? ? ? setContentView(R.layout.activity_flip) ? ? ? ? //給viewFlipper設(shè)置進(jìn)出場的動畫格式 ? ? ? ?viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.bottom_in)) viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.top_out)) ? ? ? ? //使用kotlin動態(tài)的創(chuàng)建textview的對象 ? ? ? ? var textview: TextView = TextView(this) ? ? ? ? //kotlin中使用的是直接如下 ?.屬性 來設(shè)置的,不再用setxxx設(shè)置屬性 ? ? ? ? textview.text = "這是一個動態(tài)添加的標(biāo)題xxxx" ? ? ? ? textview.textSize = 25f ? ? ? ? textview.setTextColor(R.color.blue) ? ? ? ? textview.gravity = Gravity.CENTER ? ? ? ? viewFlipper.addView(textview) //動態(tài)添加一條textview(靜態(tài)動態(tài)添加都可以) ? ? ? ? viewFlipper.startFlipping() ?//啟動viewflipper ? ? } }
在kotlin中可以直接使用 import kotlinx.android.synthetic.main.activity_flip.* 語句導(dǎo)入,之后就可以入代碼中直接書寫viewFlipper 不用在進(jìn)行findviewbyid進(jìn)行初始化了;
其中給viewFlipper設(shè)置進(jìn)出場動畫如下:
bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <translate ? ? ? ? android:duration="1000" ? ? ? ? android:fromYDelta="100%p" ? ? ? ? android:toYDelta="0"/> </set>
top_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <translate ? ? ? ? android:duration="1000" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" /> </set>
以上就是多行文本的上下滾動的實(shí)現(xiàn),如果想實(shí)現(xiàn)其他的動畫切換可以更改 動畫xml進(jìn)行實(shí)現(xiàn)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)點(diǎn)贊控件
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)點(diǎn)贊控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11Android?Jetpack結(jié)構(gòu)運(yùn)用Compose實(shí)現(xiàn)微博長按點(diǎn)贊彩虹效果
Compose在動畫方面下足了功夫,提供了豐富的API。但也正由于API種類繁多,如果想一氣兒學(xué)下來,最終可能會消化不良,導(dǎo)致似懂非懂。結(jié)合例子學(xué)習(xí)是一個不錯的方法,本文就帶大家邊學(xué)邊做,通過實(shí)現(xiàn)一個微博長按點(diǎn)贊的動畫效果,學(xué)習(xí)了解Compose動畫的常見思路和開發(fā)技巧2022-07-07Kotlin超簡單實(shí)現(xiàn)StepView的方法
這篇文章主要介紹了Kotlin超簡單實(shí)現(xiàn)StepView的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11解決Android SELinux權(quán)限問題記錄分析
這篇文章主要為大家介紹了解決Android SELinux權(quán)限問題記錄分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Android彈出dialog后無法捕捉back鍵的解決方法
這篇文章主要為大家詳細(xì)介紹了Android彈出dialog后無法捕捉back鍵的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09HttpClient通過Post上傳文件的實(shí)例代碼
這篇文章主要介紹了HttpClient通過Post上傳文件的實(shí)例代碼的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08Android開發(fā)實(shí)現(xiàn)去除bitmap無用白色邊框的方法示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)去除bitmap無用白色邊框的方法,結(jié)合實(shí)例形式給出了Android去除bitmap無用白色邊框的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法
這篇文章主要介紹了listView的item中有checkbox,導(dǎo)致setOnItemClick失效的原因及解決辦法,需要的朋友可以參考下2017-01-01