[Taxi passenger V1.1.0] 增加 textview 渐变
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package com.mogo.och.taxi.passenger.ui;
|
||||
|
||||
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;
|
||||
import androidx.appcompat.widget.AppCompatTextView;
|
||||
|
||||
/**
|
||||
* @author: wangmingjun
|
||||
* @date: 2022/3/22
|
||||
*/
|
||||
public class GradientTextView extends AppCompatTextView {
|
||||
|
||||
private LinearGradient mLinearGradient;
|
||||
private Paint mPaint;
|
||||
// private Paint strokPaint;
|
||||
private int mViewWidth = 0;//文字的宽度
|
||||
private int mViewHeight = 0;//文字的高度
|
||||
private Rect mTextBound = new Rect();
|
||||
private int[] mColorList;//存放颜色的数组
|
||||
private boolean isVertrial;//默认是横向
|
||||
|
||||
private float mRadius;
|
||||
private float mdx;
|
||||
private float mdy;
|
||||
private int mColor;
|
||||
|
||||
public GradientTextView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public GradientTextView(Context context,
|
||||
AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
//设置默认的颜色
|
||||
mColorList = new int[]{0xFFFFFFFF, 0xFFFFFFF};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
|
||||
if (isVertrial) {
|
||||
mViewHeight = getMeasuredHeight();
|
||||
} else {
|
||||
mViewWidth = getMeasuredWidth();
|
||||
}
|
||||
// strokPaint = getPaint();
|
||||
mPaint = getPaint();
|
||||
String mTipText = getText().toString();
|
||||
mPaint.getTextBounds(mTipText, 0, mTipText.length(), mTextBound);
|
||||
//前面4个参数分别表示渐变的开始x轴,开始y轴,结束的x轴,结束的y轴,mcolorList表示渐变的颜色数组
|
||||
mLinearGradient = new LinearGradient(0, 0, mViewWidth, mViewHeight, mColorList, null, Shader.TileMode.CLAMP);
|
||||
mPaint.setShader(mLinearGradient);
|
||||
|
||||
// if (mViewWidth == 0) {
|
||||
// measure(0, 0);
|
||||
// mViewWidth = (int) (getMeasuredWidth() + strokPaint.getStrokeWidth() * 2);
|
||||
// setWidth(mViewWidth);
|
||||
// }
|
||||
// float y = getBaseline();
|
||||
// float x = (mViewWidth - strokPaint.measureText(mTipText)) / 2;
|
||||
//
|
||||
mPaint.setShadowLayer(mRadius, mdx, mdy, mColor);
|
||||
|
||||
// //画出投影
|
||||
// canvas.drawText(mTipText, x, y, strokPaint);
|
||||
//画出文字
|
||||
canvas.drawText(mTipText, getMeasuredWidth() / 2 - mTextBound.width() / 2, getMeasuredHeight() / 2 + mTextBound.height() / 2, mPaint);
|
||||
}
|
||||
|
||||
/**
|
||||
* true表示纵向渐变,false变身横向渐变
|
||||
*
|
||||
* @param vertrial
|
||||
*/
|
||||
public void setVertrial(boolean vertrial) {
|
||||
isVertrial = vertrial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置渐变的颜色
|
||||
*
|
||||
* @param mColorList
|
||||
*/
|
||||
public void setmColorList(int[] mColorList) {
|
||||
if (mColorList != null && mColorList.length < 2) {
|
||||
throw new RuntimeException("mClorList's length must be > 2");
|
||||
} else {
|
||||
|
||||
this.mColorList = mColorList;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param strokeColor
|
||||
* @param strokewidth
|
||||
* @param shader
|
||||
*/
|
||||
// public void setStyle(int strokeColor, float strokewidth, Shader shader) {
|
||||
// strokPaint.setAntiAlias(true);
|
||||
// // 设定是否使用图像抖动处理,会使绘制出来的图片颜色更加平滑和饱满,图像更加清晰
|
||||
// strokPaint.setDither(true);
|
||||
// // 如果该项设置为true,则图像在动画进行中会滤掉对Bitmap图像的优化操作,加快显示
|
||||
// // 速度,本设置项依赖于dither和xfermode的设置
|
||||
// strokPaint.setFilterBitmap(true);
|
||||
//
|
||||
// strokPaint.setStrokeWidth(strokewidth);
|
||||
// strokPaint.setColor(strokeColor);
|
||||
// // 设置绘制时各图形的结合方式,如平滑效果等
|
||||
// strokPaint.setStrokeJoin(Paint.Join.ROUND);
|
||||
// // 当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式
|
||||
// // Cap.ROUND,或方形样式Cap.SQUARE
|
||||
// strokPaint.setStrokeCap(Paint.Cap.ROUND);
|
||||
// strokPaint.setStyle(Paint.Style.STROKE);
|
||||
//
|
||||
// float textsize = getTextSize();
|
||||
// strokPaint.setTextSize(textsize);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 设置投影层
|
||||
* @param radius
|
||||
* @param dx
|
||||
* @param dy
|
||||
* @param color
|
||||
*/
|
||||
public void setShadowLayerCustom(float radius, float dx, float dy, int color) {
|
||||
this.mRadius = radius;
|
||||
this.mdx = dx;
|
||||
this.mdy = dy;
|
||||
this.mColor = color;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user