概述
/*
* @author 林雷
* @mail 374353845@qq.com
* 渐变色TextVIew
* 注意:xml中添加的是 xmlns:gradient="http://schemas.android.com/apk/res-auto" 别添加错误了
*/
public class GradientTextView extends AppCompatTextView {
private int startColor = 0;
private int centerColor = 0;
private int endColor = 0;
private boolean isHorizontal;
/**
* 手动刷新布局 调起 onLayout()
*/
private boolean refreshLayout = false;
/**
* 控件宽度
*/
private float width = 0f;
/**
* 控件高度
*/
private float height = 0f;
private Paint paint;
/**
* 渐变颜色值数组
*/
private int[] gradientColors = new int[]{
Color.BLACK, Color.BLACK, Color.BLACK};
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context,
AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public GradientTextView(Context context,
AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttrs(context, attrs);
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.GradientTextView);
String orientation = ta.getString(R.styleable.GradientTextView_orientation);
isHorizontal = "2".equals(orientation) ? false : true;
startColor = ta.getColor(R.styleable.GradientTextView_startColor, 0);
centerColor = ta.getColor(R.styleable.GradientTextView_centerColor, 0);
endColor = ta.getColor(R.styleable.GradientTextView_endColor, 0);
gradientColors[0] = startColor;
gradientColors[1] = centerColor;
gradientColors[2] = endColor;
ta.recycle();
paint = getPaint();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed || refreshLayout) {
width = getWidth();
height = getHeight();
drawTextView();
}
}
private void drawTextView() {
if (isHorizontal) {
if (centerColor == 0) {
paint.setShader(new LinearGradient(0, 0, width, 0, startColor, endColor, Shader.TileMode.REPEAT));
} else {
KLog.i();
paint.setShader(new LinearGradient(0, 0, width, 0, gradientColors, null, Shader.TileMode.REPEAT));
}
} else {
if (centerColor == 0) {
paint.setShader(new LinearGradient(0, 0, 0, height, startColor, endColor, Shader.TileMode.REPEAT));
} else {
paint.setShader(new LinearGradient(0, 0, 0, height, gradientColors, null, Shader.TileMode.REPEAT));
}
}
refreshLayout = false;
}
private void setStartColor(int startColor) {
this.startColor = getContext().getResources().getColor(startColor);
gradientColors[0] = startColor;
}
private void setCenterColor(int centerColor) {
this.centerColor = getContext().getResources().getColor(centerColor);
gradientColors[1] = centerColor;
}
private void setEndColor(int endColor) {
this.endColor = getContext().getResources().getColor(endColor);
gradientColors[2] = endColor;
}
public void setGradientColor(int startColor, int endColor) {
setStartColor(startColor);
setEndColor(endColor);
refreshLayout = true;
requestLayout();
}
public void setGradientColors(int startColor, int centerColor, int endColor) {
setStartColor(startColor);
setCenterColor(centerColor);
setEndColor(endColor);
refreshLayout = true;
requestLayout();
}
}
使用:
<com.yang.commonUtils.view.GradientTextView
android:id="@+id/txtGetGift"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:background="@drawable/rr_s_r100cffffff"
android:gravity="center"
android:text="已领取"
android:textColor="@android:color/white"
android:textSize="12sp"
gradient:orientation="horizontal" />
<declare-styleable name="GradientTextView">
<attr name="startColor" format="color"/>
<attr name="centerColor" format="color"/>
<attr name="endColor" format="color"/>
<attr name="orientation">
<enum name="horizontal" value="1" />
<enum name="vertical" value="2" />
</attr>
</declare-styleable>
1.两个方法设置渐变色
setGradientColor(int startColor, int endColor)
setGradientColors(int startColor, int centerColor, int endColor)
2.在列表中使用,红色为关键代码
if (已领取) {
helper.setText(R.id.txtGetGift, "已领取");
helper.setBackgroundRes(R.id.txtGetGift, R.drawable.rr_r100cfea832_w1cff4848);
txtGetGift.setGradientColor(R.color.fea832, R.color.ff4848); //设置相关颜色
} else if (已领完) {
helper.setText(R.id.txtGetGift, "已领完");
helper.setBackgroundRes(R.id.txtGetGift, R.drawable.rr_s_r100cffffff);
txtGetGift.setGradientColor(R.color.select_gray_font, R.color.select_gray_font);//设置相关颜色
} else { //领取
helper.setText(R.id.txtGetGift, "领取");
helper.setBackgroundRes(R.id.txtGetGift, R.drawable.rr_r100cfea832_ff4848);
txtGetGift.setGradientColor(R.color.white, R.color.white);//设置相关颜色
}
效果
注:该控件只设置文字渐变,背景另外设置,这个矩形框是一个xml
ps:改控件是基于百度到的博客进行修改,增加动态更改样式的功能, 使其适用于列表等需求,支持java代码设置参数。
最后
以上就是舒心信封为你收集整理的自定义 GradientTextView 渐变色文字的全部内容,希望文章能够帮你解决自定义 GradientTextView 渐变色文字所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复