效果图
1. 文字渐变
private fun setGradientForTextView(textView: TextView) {
val endX = textView.paint.textSize * textView.text.length
val linearGradient = LinearGradient(
0f, 0f, endX, 0f,
Color.parseColor("#FFFF68FF"),
Color.parseColor("#FFFED732"),
Shader.TileMode.CLAMP
)
textView.paint.shader = linearGradient
textView.invalidate()
}
- 图片渐变
maven { url 'https://www.jitpack.io' }
implementation 'com.gitee.cxyzy1:AndroidGradientImageView:1.0.0'
<kr.pe.burt.android.lib.androidgradientimageview.AndroidGradientImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@drawable/beauty"
app:giv_endColor="#f391a9"
app:giv_endOffset="0.99"
app:giv_rotate="45.0"
app:giv_startOffset="0.3" />
- 背景渐变
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:centerColor="#f391a9"
android:endColor="#ef5b9c"
android:startColor="#f47920" />
<corners android:radius="0dip" />
</shape>
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/shape_gradient_bg" />
完整源代码
https://gitee.com/cxyzy1/gradient-demos