文字两种颜色渐变
作者:互联网
//自定义 布局中引用即可
public class TwoTextView extends TextView {
private LinearGradient mLinearGradient;
private Paint mPaint;
private int mViewWidth = 0;
private Rect mTextBound = new Rect();
public TwoTextView(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[]{Color.parseColor("#58BEFF"),
Color.parseColor("#4463FF")},
null, Shader.TileMode.REPEAT);
mPaint.setShader(mLinearGradient);
canvas.drawText(mTipText, getMeasuredWidth() / 2 - mTextBound.width() / 2, getMeasuredHeight() / 2 + mTextBound.height() / 2, mPaint);
}
}
标签:文字,颜色,mTipText,渐变,private,mViewWidth,mPaint,mTextBound,new 来源: https://blog.csdn.net/qq_46237697/article/details/119773321