服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 使用Kotlin实现文字渐变TextView的代码

使用Kotlin实现文字渐变TextView的代码

2022-12-13 16:34zhangmeiwen95 Android

这篇文章主要介绍了使用Kotlin实现文字渐变TextView的代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

实现效果:

使用Kotlin实现文字渐变TextView的代码

实现代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import android.content.Context
import android.graphics.*
import android.support.annotation.ColorInt
import android.support.annotation.ColorRes
import android.text.TextPaint
import android.util.AttributeSet
import android.widget.TextView
import com.ans.utilactivity.R
 
 
class GradientTextView @JvmOverloads constructor(
  context: Context?,
  attrs: AttributeSet? = null
) : TextView(context, attrs) {
 
 
  private var mPaint: TextPaint? = null
  private var mLinearGradient: LinearGradient? = null
  private var mMeasureWidth = 0
  private var mTextMatrix: Matrix? = null
 
  @ColorInt
  private var mStartColor: Int = 0xFF333333.toInt()
  @ColorInt
  private var mEndColor: Int = 0xFF333333.toInt()
 
  init {
    if (attrs != null) {
      val attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.GradientTextView)
      mStartColor = attrArray.getColor(R.styleable.GradientTextView_startColor, mStartColor)
      mEndColor = attrArray.getColor(R.styleable.GradientTextView_endColor, mEndColor)
    }
  }
 
  /**
   * 复写onSizeChanged方法
   *
   */
  override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    super.onSizeChanged(w, h, oldw, oldh)
    mMeasureWidth = measuredWidth
    if (mMeasureWidth > 0) {
      mPaint = paint
      //(x0,y0):渐变起始点坐标
      //(x1,y1):渐变结束点坐标
      //color0:渐变开始点颜色,16进制的颜色表示,必须要带有透明度
      //color1:渐变结束颜色
      //colors:渐变数组
      //positions:位置数组,position的取值范围[0,1],作用是指定某个位置的颜色值,如果传null,渐变就线性变化。
      //tile:用于指定控件区域大于指定的渐变区域时,空白区域的颜色填充方法。
      mLinearGradient = LinearGradient(
        0f
        , 0f
        , mMeasureWidth.toFloat()
        , 0f
        , intArrayOf(mStartColor, mEndColor)
        , null
        , Shader.TileMode.CLAMP
      )
      mPaint?.shader = mLinearGradient
      mTextMatrix = Matrix()
    }
  }
}

attr.xml 引用

?
1
2
3
4
<declare-styleable name="GradientTextView">
  <attr name="startColor" format="color"/>
  <attr name="endColor" format="color"/>
</declare-styleable>

引用:

?
1
2
3
4
5
6
7
<前缀.GradientTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:startColor="@color/colorPrimary"
    app:endColor="@color/colorAccent"
    />

到此这篇关于使用Kotlin实现文字渐变TextView的文章就介绍到这了,更多相关Kotlin文字渐变TextView内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/zhangmeiwen95/article/details/105766074

延伸 · 阅读

精彩推荐
  • AndroidAndroid实现获取SERIAL信息的方法

    Android实现获取SERIAL信息的方法

    这篇文章主要介绍了Android实现获取SERIAL信息的方法,涉及Android操作SERIAL的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    androidyue11082021-04-02
  • AndroidAndroid开发学习路线图

    Android开发学习路线图

    这篇文章主要介绍了Android开发学习路线图,本文从基础、入门、进阶、高级等4个方面铺开学习路线图,需要的朋友可以参考下...

    Android教程网5872021-03-17
  • Androidflutter 怎么实现app整体灰色效果

    flutter 怎么实现app整体灰色效果

    Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,支持移动、Web、桌面和嵌入式平台。这篇文章给大家介绍flutter 怎么...

    做人要简单7862022-12-08
  • AndroidAndroid画板开发之橡皮擦功能

    Android画板开发之橡皮擦功能

    这篇文章主要为大家详细介绍了Android画板开发之橡皮擦功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    tpnet9972022-09-03
  • Android解析android中系统日期时间的获取

    解析android中系统日期时间的获取

    本篇文章是对在android中,如何系统日期时间获取的方法进行了详细的分析介绍,需要的朋友参考下...

    Android开发网4892021-01-30
  • AndroidAndroid基础控件(EditView、SeekBar等)的使用方法

    Android基础控件(EditView、SeekBar等)的使用方法

    这篇文章主要介绍了Android基础控件的属性及使用方法,介绍了基础控件有TextView、ImageView、Button、EditView等,感兴趣的小伙伴们可以参考一下...

    呆尐兔兔6402021-06-23
  • AndroidAndroid启动相机拍照并返回图片

    Android启动相机拍照并返回图片

    Android启动相机拍照并返回图片,事先定义拍照方法,在启动拍照之前先判断内存是否可用,通过重写onactivityresult()方法,获取拍好的图片。对下文感兴趣的...

    Android开发网4642021-04-07
  • Android在android中实现类似uc和墨迹天气的左右拖动效果

    在android中实现类似uc和墨迹天气的左右拖动效果

    本文主要介绍下怎样在android实现uc和墨迹天气那样的左右拖动效果,具体代码如下,感兴趣的朋友可以参考下哈...

    Android开发网8302021-02-02