软件编程
位置:首页>> 软件编程>> Android编程>> Android TextView渐变颜色和方向及动画效果的设置详解

Android TextView渐变颜色和方向及动画效果的设置详解

作者:Lucky_William  发布时间:2022-06-13 09:23:58 

标签:Android,TextView,渐变颜色,动画效果

GradientTextView

Github点我

一个非常好用的库,使用kotlin实现,用于设置TexView的字体 渐变颜色渐变方向动画效果

添加依赖

之前仓库发布在 jcenter,但是因为它即将不可用,近期已完成迁移。建议大家使用 mavenCentral 的配置。

  • 使用 jcenter


implementation 'com.williamyang:gradienttext:1.0.1'
  • 使用 mavenCentral


buildscript {
   repositories {
       mavenCentral()
   }
}

implementation 'io.github.weilianyang:gradienttext:1.0.1'

效果预览:

Android TextView渐变颜色和方向及动画效果的设置详解

Android TextView渐变颜色和方向及动画效果的设置详解

一、控件样式


<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_directionleftToRight:从左向右,topToBottom:从上向下渐变颜色的方向(默认值:leftToRight)
gradient_animatetrue or false渐变颜色的动画开关(默认值:false)
gradient_speedslow、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()
}

来源:https://blog.csdn.net/java_android_man/article/details/107290786

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com