文字颜色渐变效果图

下图中那串数字就处于重写的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));        }    }}

 

更多相关文章

  1. Android(安卓)NDK 错误解决方法(持续更新ing)
  2. 管理应用自启动的方案
  3. Android时间倒计时
  4. android退出应用程序解决方案
  5. Android使用selector改变和文本框文字的颜色
  6. Android(安卓)RecycerView 中根据图片大小自适应控件大小的实现
  7. Android(安卓)ListView拖动时背景颜色会变成黑色 的解决办法
  8. Android(安卓)Service不被杀死
  9. Android(安卓)Studio Error:Connection timed out: connect.解决

随机推荐

  1. Android(安卓)studio中获取数字签名(SAH1)
  2. 【Android(安卓)开发教程】Notification
  3. 【UI交互效果】android UI效果一: coverF
  4. Android数据篇(二)
  5. 初探Android平台上的定位服务(GPS)
  6. Android开发:TabActivity中onKeyDown无法
  7. Android程序框架
  8. android 监听网络状态
  9. Eclipse使用Butterknife的相关配置
  10. adw-launcher-android----流行的桌面