文本渐变色

产品设计遇到了文本渐变和背景渐变,效果图
在这里插入图片描述

遇到问题

  • 渐变失效
  • 渐变色太浅

解决

  • 没有默认颜色,渐变色就会很淡
  • textview最好wrapcontent或者准确的dp值,如果设置成match_parent渐变会失效但是文本内容宽度只有一小部分那么渐变会失效
    (因为是根据文本长度进行渐变的,如果宽度与实际不一致就会出现失效的问题)
  • 因为2所以外层一般需要包裹一层

工具类

 public class TextViewUtils {
    public static void setGradientForTextView(TextView textView) {
        float endX = textView.getPaint().getTextSize() * textView.getText().length();
        LinearGradient linearGradient = new LinearGradient(
                0f, 0f, endX, 0f,
                Color.parseColor("#FF63CEFF"),
                Color.parseColor("#FF5E63FC"),
                Shader.TileMode.CLAMP
        );
        textView.getPaint().setShader(linearGradient);
        textView.invalidate();
    }

}

部分布局代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/C_999999"
            android:textSize="18sp" />

        <View
            android:id="@+id/view_line"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/C_DDDDDD" />

        <LinearLayout
            android:id="@+id/ll_confirm"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:id="@+id/tv_confirm"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="确定"
                android:textColor="@color/C_5E6AFC"
                android:textSize="18sp" />
        </LinearLayout>

    </LinearLayout>

使用

TextViewUtils.setGradientForTextView(tvConfirm);

仅此记录,如果有更好的实现方法,还请留言指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值