Android TextView漸變顏色和方向及動畫效果的設置詳解
更新時間:2021年11月06日 14:30:07 作者:Lucky_William
TextView的在安卓中可以理解為一個文本視圖控件,Android的視圖控件的基類是View類,可以理解的TextView是View的子類。我們通常在.XML布局文件中會為文本視圖控件指定各種屬性來設置它的樣式,今天我們要講的當然不是傳統(tǒng)常見的那種,將會帶有漸變顏色和方向及動畫效果
GradientTextView
一個非常好用的庫,使用kotlin實現(xiàn),用于設置TexView的字體 漸變顏色、漸變方向 和 動畫效果
添加依賴
之前倉庫發(fā)布在 jcenter,但是因為它即將不可用,近期已完成遷移。建議大家使用 mavenCentral 的配置。
- 使用 jcenter
implementation 'com.williamyang:gradienttext:1.0.1'
- 使用 mavenCentral
buildscript {
repositories {
mavenCentral()
}
}
implementation 'io.github.weilianyang:gradienttext:1.0.1'
效果預覽:


一、控件樣式
<declare-styleable name="GradientTextView">
<attr name="gradient_startColor" format="reference|color" />
<attr name="gradient_endColor" format="reference|color" />
<attr name="gradient_direction" format="enum">
<enum name="leftToRight" value="1" />
<enum name="topToBottom" value="2" />
</attr>
<attr name="gradient_animate" format="boolean" />
<attr name="gradient_speed" format="enum">
<enum name="slow" value="20" />
<enum name="normal" value="10" />
<enum name="fast" value="5" />
</attr>
</declare-styleable>
二、屬性介紹
| 屬性 | 可選值 | 作用 |
|---|---|---|
| gradient_startColor | 值在color.xml中定義 | 漸變顏色的起始值(默認值:#6200EE) |
| gradient_endColor | 值在color.xml中定義 | 漸變顏色的結束值(默認值:#03DAC5) |
| gradient_direction | leftToRight:從左向右,topToBottom:從上向下 | 漸變顏色的方向(默認值:leftToRight) |
| gradient_animate | true or false | 漸變顏色的動畫開關(默認值:false) |
| gradient_speed | slow、normal、fast | 漸變顏色的動畫速度(默認值:normal) |
三、控件在布局中使用
<com.william.gradient.GradientTextView
android:id="@+id/gradientTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/leftToRight_GradientTextView"
android:textSize="24sp"
app:gradient_animate="true"
app:gradient_direction="leftToRight"
app:gradient_speed="normal" />
四、在代碼中控制動畫開關
gradientTextView.apply {
translateAnimate = !translateAnimate
invalidate()
}
到此這篇關于Android TextView漸變顏色和方向及動畫效果的設置詳解的文章就介紹到這了,更多相關Android TextView內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android 定位系統(tǒng)(GPS)開發(fā)詳解
GPS定位是智能手機上一個比較有意思的功能,LBS等服務都有效的利用了GPS定位功能,本文就跟大家分享下Android開發(fā)中的GPS定位知識2016-07-07
Android開發(fā)之ToggleButton實現(xiàn)開關效果示例
這篇文章主要介紹了Android開發(fā)之ToggleButton實現(xiàn)開關效果的方法,結合實例形式分析了ToggleButton控件實現(xiàn)開關效果的布局與功能相關操作技巧,需要的朋友可以參考下2017-07-07

