Android TextView 文字渐变
自定义可以使用Drawable填充文字的TextView
使用:
<com.example.cc.drawabletextview.DrawableTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="64sp"
android:textStyle="bold"
app:textDrawable="自定义填充文字的Drawable" />
截图:
原理:自定义TextView,设置一个Drawable来生成一个着色器(Shader)通过TextView的getPaint().setShader()方法为TextView设置文字的着色器来实现渐变、图片填充等效果。
支持的Drawable类型有:
- ColorDrawable:使用颜色填充(和setTextColor效果一致)
- BitmapDrawable:使用图片填充
- GradientDrawable:渐变填充
- StateListDrawable:即Selector (selector 里的item也必须是以上三种Drawable)