Android Textview实现文字颜色渐变效果

欢迎大家关注我的公众号:**牛角尖尖上起舞**

文字颜色渐变效果图

下图中那串数字就处于重写的TextView中:

TextView文字颜色渐变-效果图

实现方案

方案一:继承TextView,重写onDraw()方法

import android.widget.TextView;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;

/**
 * Created by ZuoHailong on 2017/11/14.
 */

public class GradientColorTextView extends TextView {

    private LinearGradient mLinearGradient;
    private Paint mPaint;
    private int mViewWidth = 0;
    private Rect mTextBound = new Rect();

    public GradientColorTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mViewWidth = getMeasuredWidth();
        mPaint = getPaint();
        String mTipText = getText().toString();
        mPaint.getTextBounds(mTipText, 0, mTipText.length(), mTextBound);
        mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0,
                new int[]{0xFFFFEABA, 0xFFBE8B49},
                null, Shader.TileMode.REPEAT);
        mPaint.setShader(mLinearGradient);
        canvas.drawText(mTipText, getMeasuredWidth() / 2 - mTextBound.width() / 2, getMeasuredHeight() / 2 + mTextBound.height() / 2, mPaint);
    }
}

方案二:继承TextView,重写onLayout()方法。

要注意的是,TextView不可以设置TextColor属性了,否则与onLayout()中设置的渐变颜色叠加渲染,会出现色差。

import android.widget.TextView;
import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.AttributeSet;

/**
 * Created by ZuoHailong on 2017/11/14.
 */

public class GradientColorTextViewForManagerName extends TextView {
    public GradientColorTextViewForManagerName(Context context) {
        super(context);
    }

    public GradientColorTextViewForManagerName(Context context,
                                               AttributeSet attrs) {
        super(context, attrs);
    }
    public GradientColorTextViewForManagerName(Context context,
                                               AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed,
                            int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            getPaint().setShader(new LinearGradient(
                    0, 0, getWidth(), getHeight(),
                    new int[]{0xFFFFEABA, 0xFFDFBB82, 0xFFBE8B49}, new float[]{0, 0.5f, 1},
                    Shader.TileMode.CLAMP));
        }
    }
}

欢迎大家关注我的公众号:**牛角尖尖上起舞**

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 AndroidTextView 实现字体颜色渐变效果,你可以使用 SpannableString 和 ForegroundColorSpan 组合来实现。下面是一个示例代码: ```java TextView textView = findViewById(R.id.textView); String text = "Hello World!"; // 创建一个 SpannableString 对象 SpannableString spannableString = new SpannableString(text); // 定义渐变起始颜色和结束颜色 int startColor = Color.RED; int endColor = Color.BLUE; // 创建一个 ForegroundColorSpan 对象,并将其应用到 SpannableString ForegroundColorSpan colorSpan = new ForegroundColorSpan(startColor); spannableString.setSpan(colorSpan, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 创建一个渐变效果的 Span Shader shader = new LinearGradient(0, 0, 0, textView.getPaint().getTextSize(), startColor, endColor, Shader.TileMode.CLAMP); ShaderSpan shaderSpan = new ShaderSpan(shader); // 将渐变效果的 Span 应用到 SpannableString spannableString.setSpan(shaderSpan, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置 TextView 的文本为 SpannableString textView.setText(spannableString); ``` 在上面的示例,我们首先创建了一个 SpannableString 对象,然后定义了起始颜色和结束颜色。接着,我们创建了一个 ForegroundColorSpan 对象,并将其应用到 SpannableString ,以实现初始字体颜色。然后,我们创建了一个 LinearGradient 对象,并使用 ShaderSpan 来应用渐变效果。最后,我们将 SpannableString 设置为 TextView 的文本,实现字体颜色渐变效果。 请注意,这种方式只能实现单一的线性渐变效果。如果你想要实现更复杂的渐变效果,可能需要自定义 View 或使用第三方库。 希望这个示例对你有帮助。如果你有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值