自定义 GradientTextView 渐变色文字


/*
 * @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代码设置参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值