mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-14 05:27:10 +08:00
源码整理
This commit is contained in:
@@ -13,87 +13,79 @@ import android.os.Message;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
import android.view.View;
|
||||
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/03/19 14:04:20
|
||||
* @Author 豆包&Big Pickle&MiMo&ZhanGSKen<zhangsken@qq.com>
|
||||
* @CreateDate 2025/03/19 14:04:20
|
||||
* @LastEdit 2026/07/21 11:40:00
|
||||
* @Describe 云盾华氏度热力视图,垂直盾值温度视图控件(带颜色渐变+静态Handler更新)
|
||||
* 采用绘图方式展示盾值温度,填充色随盾值比例渐变,支持设置文本在温度条左侧/右侧,底部对齐竖排显示
|
||||
* 温度条宽度=5dp,文本区宽度固定=5dp,整体左右边距=0,无任何多余空白间距
|
||||
* 1. 原生Canvas绘图渲染盾值温度条,填充色随盾值比例动态渐变
|
||||
* 2. 支持竖排数值文本切换至温度条左侧/右侧,文本底部对齐展示
|
||||
* 3. 固定尺寸规范:温度条宽度5dp、文本区域宽度5dp,控件左右无外边距
|
||||
* 4. 全局静态Handler统一分发盾值更新,WeakHashMap弱引用缓存控件防止内存泄漏
|
||||
* 5. 代码完全兼容Java7语法,无Lambda、无try-with-resources、无高版本API
|
||||
*/
|
||||
public class DunTemperatureView extends View {
|
||||
// ====================== 常量定义区 ======================
|
||||
public static final String TAG = "DunTemperatureView";
|
||||
// 控件默认高度
|
||||
private static final int DEFAULT_HEIGHT = 200;
|
||||
// 温度条宽度(5dp)、文本区宽度(固定5dp)
|
||||
private static final int THERMOMETER_WIDTH_DP = 5;
|
||||
private static final int TEXT_AREA_WIDTH_DP = 5;
|
||||
// 填充区域内边距(左右=0,上下=2避免贴边)
|
||||
private static final int FILL_PADDING_HORIZONTAL = 0;
|
||||
private static final int FILL_PADDING_VERTICAL = 2;
|
||||
// 竖排文本字间距
|
||||
private static final float TEXT_CHAR_SPACING = 8f;
|
||||
// 渐变颜色常量(低→中→高 对应绿→黄→红)
|
||||
private static final int COLOR_GRADIENT_LOW = Color.GREEN;
|
||||
private static final int COLOR_GRADIENT_MID = Color.YELLOW;
|
||||
private static final int COLOR_GRADIENT_HIGH = Color.RED;
|
||||
// 边框颜色
|
||||
private static final int COLOR_BORDER = Color.parseColor("#FF444444");
|
||||
// 文本颜色
|
||||
private static final int COLOR_TEXT = Color.parseColor("#FF000000");
|
||||
// Handler消息标识
|
||||
public static final int MSG_UPDATE_DUN_VALUE = 0x01;
|
||||
// 消息参数Key
|
||||
public static final String KEY_MAX_VALUE = "max_value";
|
||||
public static final String KEY_CURRENT_VALUE = "current_value";
|
||||
|
||||
private static final int DEFAULT_HEIGHT = 200;
|
||||
private static final int THERMOMETER_WIDTH_DP = 5;
|
||||
private static final int TEXT_AREA_WIDTH_DP = 5;
|
||||
private static final int FILL_PADDING_HORIZONTAL = 0;
|
||||
private static final int FILL_PADDING_VERTICAL = 2;
|
||||
private static final float TEXT_CHAR_SPACING = 8f;
|
||||
|
||||
private static final int COLOR_GRADIENT_LOW = Color.GREEN;
|
||||
private static final int COLOR_GRADIENT_MID = Color.YELLOW;
|
||||
private static final int COLOR_GRADIENT_HIGH = Color.RED;
|
||||
private static final int COLOR_BORDER = Color.parseColor("#FF444444");
|
||||
private static final int COLOR_TEXT = Color.parseColor("#FF000000");
|
||||
|
||||
// ====================== 静态成员区 ======================
|
||||
// 弱引用缓存控件实例,避免内存泄漏
|
||||
private static WeakHashMap<DunTemperatureView, Object> sViewCache = new WeakHashMap<>();
|
||||
// 静态Handler,处理跨线程更新消息
|
||||
private static final WeakHashMap<DunTemperatureView, Object> sViewCache = new WeakHashMap<DunTemperatureView, Object>();
|
||||
private static Handler sStaticHandler;
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
// 画笔相关
|
||||
// ====================== 实例成员变量区 ======================
|
||||
private Paint mThermometerPaint;
|
||||
private Paint mFillPaint;
|
||||
private Paint mTextPaint;
|
||||
// 尺寸参数(dp转px后的值)
|
||||
|
||||
private int mThermometerWidth;
|
||||
private int mTextAreaWidth;
|
||||
private int mMaxValue = 100; // 最高盾值
|
||||
private int mCurrentValue = 0; // 当前盾值
|
||||
private RectF mThermometerRect; // 温度条矩形区域
|
||||
// 渐变颜色配置(低→中→高 对应绿→黄→红)
|
||||
private int[] mGradientColors = {COLOR_GRADIENT_LOW, COLOR_GRADIENT_MID, COLOR_GRADIENT_HIGH};
|
||||
private float[] mGradientPositions = {0.0f, 0.5f, 1.0f};
|
||||
// 布局配置:true=文本在温度条右侧(默认),false=文本在温度条左侧
|
||||
private int mMaxValue = 100;
|
||||
private int mCurrentValue = 0;
|
||||
|
||||
private RectF mThermometerRect;
|
||||
private int[] mGradientColors = new int[]{COLOR_GRADIENT_LOW, COLOR_GRADIENT_MID, COLOR_GRADIENT_HIGH};
|
||||
private float[] mGradientPositions = new float[]{0.0f, 0.5f, 1.0f};
|
||||
private boolean isTextOnRight = true;
|
||||
// 其他颜色配置
|
||||
private int mBorderColor = COLOR_BORDER;
|
||||
private int mTextColor = COLOR_TEXT;
|
||||
|
||||
// ====================== 静态代码块 ======================
|
||||
static {
|
||||
// 初始化静态Handler,绑定主线程Looper
|
||||
sStaticHandler = new Handler(Looper.getMainLooper()) {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
public void handleMessage(final Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == MSG_UPDATE_DUN_VALUE) {
|
||||
// 获取消息中的盾值参数
|
||||
int maxValue = msg.getData().getInt(KEY_MAX_VALUE, 100);
|
||||
int currentValue = msg.getData().getInt(KEY_CURRENT_VALUE, 0);
|
||||
LogUtils.d(TAG, "sStaticHandler: 收到更新消息,max=" + maxValue + ", current=" + currentValue);
|
||||
final int targetMsgWhat = msg.what;
|
||||
if (targetMsgWhat == MSG_UPDATE_DUN_VALUE) {
|
||||
final int maxValue = msg.getData().getInt(KEY_MAX_VALUE, 100);
|
||||
final int currentValue = msg.getData().getInt(KEY_CURRENT_VALUE, 0);
|
||||
LogUtils.d(TAG, "handleMessage receive update params, max=" + maxValue + ", current=" + currentValue);
|
||||
|
||||
// 遍历缓存的控件实例,更新所有实例
|
||||
for (DunTemperatureView view : sViewCache.keySet()) {
|
||||
if (view != null && view.isShown()) {
|
||||
view.setMaxValue(maxValue);
|
||||
view.setCurrentValue(currentValue);
|
||||
final DunTemperatureView targetView = view;
|
||||
if (targetView != null && targetView.isShown()) {
|
||||
targetView.setMaxValue(maxValue);
|
||||
targetView.setCurrentValue(currentValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,203 +94,194 @@ public class DunTemperatureView extends View {
|
||||
}
|
||||
|
||||
// ====================== 构造函数区 ======================
|
||||
public DunTemperatureView(Context context) {
|
||||
public DunTemperatureView(final Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
public DunTemperatureView(Context context, AttributeSet attrs) {
|
||||
public DunTemperatureView(final Context context, final AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public DunTemperatureView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
public DunTemperatureView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
// ====================== 初始化方法区 ======================
|
||||
// ====================== 初始化私有方法 ======================
|
||||
/**
|
||||
* 初始化画笔和参数
|
||||
* 初始化画笔、尺寸参数并将实例存入全局缓存
|
||||
*/
|
||||
private void init() {
|
||||
LogUtils.d(TAG, "init: 开始初始化云盾温度视图控件");
|
||||
// dp转px(适配不同分辨率)
|
||||
mThermometerWidth = dp2px(getContext(), THERMOMETER_WIDTH_DP);
|
||||
mTextAreaWidth = dp2px(getContext(), TEXT_AREA_WIDTH_DP);
|
||||
LogUtils.d(TAG, "init: 温度条宽度5dp转px=" + mThermometerWidth + ",文本区宽度5dp转px=" + mTextAreaWidth);
|
||||
final Context context = getContext();
|
||||
LogUtils.d(TAG, "init start");
|
||||
|
||||
mThermometerWidth = dp2px(context, THERMOMETER_WIDTH_DP);
|
||||
mTextAreaWidth = dp2px(context, TEXT_AREA_WIDTH_DP);
|
||||
LogUtils.d(TAG, "init dp convert result, thermometerWidth=" + mThermometerWidth + ", textAreaWidth=" + mTextAreaWidth);
|
||||
|
||||
// 温度条边框画笔(宽度1px,适配5dp宽度)
|
||||
mThermometerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mThermometerPaint.setColor(mBorderColor);
|
||||
mThermometerPaint.setStyle(Paint.Style.STROKE);
|
||||
mThermometerPaint.setStrokeWidth(1);
|
||||
|
||||
// 温度条填充画笔(支持渐变)
|
||||
mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mFillPaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
// 文本画笔(适配5dp窄文本区,文字居中绘制)
|
||||
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
mTextPaint.setColor(mTextColor);
|
||||
mTextPaint.setTextSize(18); // 缩小字号适配5dp窄文本区(避免文字超出)
|
||||
mTextPaint.setTextSize(18);
|
||||
mTextPaint.setTextAlign(Paint.Align.CENTER);
|
||||
mTextPaint.setFakeBoldText(true); // 文字加粗,提升窄区域可读性
|
||||
mTextPaint.setFakeBoldText(true);
|
||||
|
||||
// 初始化温度条矩形
|
||||
mThermometerRect = new RectF();
|
||||
|
||||
// 将当前实例加入静态缓存
|
||||
sViewCache.put(this, null);
|
||||
LogUtils.d(TAG, "init: 云盾温度视图控件初始化完成,实例已加入缓存");
|
||||
LogUtils.d(TAG, "init complete, add view to static cache");
|
||||
}
|
||||
|
||||
// ====================== 工具方法区 ======================
|
||||
/**
|
||||
* dp 转 px(适配不同屏幕分辨率)
|
||||
* dp单位转换px,多分辨率屏幕适配
|
||||
* @param context 上下文
|
||||
* @param dpValue dp数值
|
||||
* @return 对应px像素值
|
||||
*/
|
||||
private int dp2px(Context context, float dpValue) {
|
||||
return (int) TypedValue.applyDimension(
|
||||
TypedValue.COMPLEX_UNIT_DIP,
|
||||
dpValue,
|
||||
context.getResources().getDisplayMetrics()
|
||||
);
|
||||
private int dp2px(final Context context, final float dpValue) {
|
||||
final float density = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (dpValue * density + 0.5f);
|
||||
}
|
||||
|
||||
// ====================== 对外控制方法区 ======================
|
||||
// ====================== UI布局控制方法 ======================
|
||||
/**
|
||||
* 设置文本相对于温度条的位置
|
||||
* @param isOnRight true=文本在温度条右侧(默认),false=文本在温度条左侧
|
||||
* 设置文本在温度条的左右位置
|
||||
* @param isOnRight true=右侧,false=左侧
|
||||
*/
|
||||
public void setTextPosition(boolean isOnRight) {
|
||||
public void setTextPosition(final boolean isOnRight) {
|
||||
this.isTextOnRight = isOnRight;
|
||||
invalidate(); // 刷新布局绘制
|
||||
LogUtils.d(TAG, "setTextPosition input param=" + isOnRight);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前文本位置配置
|
||||
* @return true=右侧,false=左侧
|
||||
*/
|
||||
public boolean isTextOnRight() {
|
||||
return isTextOnRight;
|
||||
}
|
||||
|
||||
// ====================== 对外静态方法区 ======================
|
||||
// ====================== 全局静态更新入口 ======================
|
||||
/**
|
||||
* 静态外部方法:发送消息更新所有 DunTemperatureView 实例的盾值
|
||||
* 可在子线程中调用
|
||||
* @param maxValue 最高盾值
|
||||
* 全局静态方法,统一更新所有控件盾值,支持子线程调用
|
||||
* @param maxValue 最大盾值
|
||||
* @param currentValue 当前盾值
|
||||
*/
|
||||
public static void updateDunValue(int maxValue, int currentValue) {
|
||||
public static void updateDunValue(final int maxValue, final int currentValue) {
|
||||
if (sStaticHandler == null) {
|
||||
LogUtils.w(TAG, "updateDunValue: 静态Handler未初始化");
|
||||
LogUtils.w(TAG, "updateDunValue static handler uninitialized");
|
||||
return;
|
||||
}
|
||||
// 封装参数到消息
|
||||
Message msg = sStaticHandler.obtainMessage(MSG_UPDATE_DUN_VALUE);
|
||||
LogUtils.d(TAG, "static updateDunValue call, input max=" + maxValue + ", current=" + currentValue);
|
||||
final Message msg = sStaticHandler.obtainMessage(MSG_UPDATE_DUN_VALUE);
|
||||
msg.getData().putInt(KEY_MAX_VALUE, maxValue);
|
||||
msg.getData().putInt(KEY_CURRENT_VALUE, currentValue);
|
||||
// 发送消息
|
||||
sStaticHandler.sendMessage(msg);
|
||||
}
|
||||
|
||||
// ====================== 对外实例方法区 ======================
|
||||
// ====================== 盾值读写实例方法 ======================
|
||||
/**
|
||||
* 设置最高盾值
|
||||
* @param maxValue 最高盾值(需大于0)
|
||||
* 设置最大盾值,非法值会拦截并打印日志
|
||||
* @param maxValue 目标最大值,必须大于0
|
||||
*/
|
||||
public void setMaxValue(int maxValue) {
|
||||
public void setMaxValue(final int maxValue) {
|
||||
if (maxValue <= 0) {
|
||||
LogUtils.w(TAG, "setMaxValue: 最高盾值必须大于0,当前值=" + maxValue);
|
||||
LogUtils.w(TAG, "setMaxValue invalid input value=" + maxValue);
|
||||
return;
|
||||
}
|
||||
this.mMaxValue = maxValue;
|
||||
// 限制当前值不超过最大值
|
||||
mCurrentValue = Math.min(mCurrentValue, maxValue);
|
||||
invalidate(); // 重绘控件
|
||||
LogUtils.d(TAG, "setMaxValue update max=" + maxValue + ", clamp current=" + mCurrentValue);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置当前盾值
|
||||
* @param currentValue 当前盾值(范围 0~maxValue)
|
||||
* 设置当前盾值,自动限制0~maxValue区间
|
||||
* @param currentValue 目标当前盾值
|
||||
*/
|
||||
public void setCurrentValue(int currentValue) {
|
||||
int oldValue = this.mCurrentValue;
|
||||
public void setCurrentValue(final int currentValue) {
|
||||
final int oldVal = this.mCurrentValue;
|
||||
this.mCurrentValue = Math.max(0, Math.min(currentValue, mMaxValue));
|
||||
if (oldValue != this.mCurrentValue) {
|
||||
LogUtils.d(TAG, "setCurrentValue: 当前盾值从" + oldValue + "更新为" + mCurrentValue);
|
||||
invalidate(); // 重绘控件
|
||||
if (oldVal != this.mCurrentValue) {
|
||||
LogUtils.d(TAG, "setCurrentValue change value from " + oldVal + " to " + mCurrentValue);
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前盾值
|
||||
*/
|
||||
public int getCurrentValue() {
|
||||
return mCurrentValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最高盾值
|
||||
*/
|
||||
public int getMaxValue() {
|
||||
return mMaxValue;
|
||||
}
|
||||
|
||||
// ====================== 渐变、颜色自定义配置 ======================
|
||||
/**
|
||||
* 设置自定义渐变颜色
|
||||
* @param colors 渐变颜色数组(至少2种颜色)
|
||||
* @param positions 颜色位置数组(与colors长度一致,0.0~1.0)
|
||||
* 自定义渐变颜色数组与位置节点
|
||||
* @param colors 颜色数组,长度≥2
|
||||
* @param positions 渐变节点数组,长度与colors一致
|
||||
*/
|
||||
public void setGradientColors(int[] colors, float[] positions) {
|
||||
public void setGradientColors(final int[] colors, final float[] positions) {
|
||||
if (colors == null || colors.length < 2 || positions == null || positions.length != colors.length) {
|
||||
LogUtils.w(TAG, "setGradientColors: 渐变颜色参数不合法,颜色数组长度=" + (colors == null ? "null" : colors.length));
|
||||
LogUtils.w(TAG, "setGradientColors invalid param, color array length=" + (colors == null ? "null" : colors.length));
|
||||
return;
|
||||
}
|
||||
this.mGradientColors = colors;
|
||||
this.mGradientPositions = positions;
|
||||
LogUtils.d(TAG, "setGradientColors: 自定义渐变颜色已设置");
|
||||
LogUtils.d(TAG, "setGradientColors custom gradient config applied");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置温度条边框颜色
|
||||
* 更新温度条边框颜色
|
||||
* @param color 目标色值
|
||||
*/
|
||||
public void setBorderColor(int color) {
|
||||
public void setBorderColor(final int color) {
|
||||
this.mBorderColor = color;
|
||||
mThermometerPaint.setColor(color);
|
||||
LogUtils.d(TAG, "setBorderColor: 边框颜色已更新");
|
||||
LogUtils.d(TAG, "setBorderColor update border color");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文本颜色
|
||||
* 更新竖排文本颜色
|
||||
* @param color 目标色值
|
||||
*/
|
||||
public void setTextColor(int color) {
|
||||
public void setTextColor(final int color) {
|
||||
this.mTextColor = color;
|
||||
mTextPaint.setColor(color);
|
||||
LogUtils.d(TAG, "setTextColor: 文本颜色已更新");
|
||||
LogUtils.d(TAG, "setTextColor update text color");
|
||||
invalidate();
|
||||
}
|
||||
|
||||
// ====================== 生命周期方法 ======================
|
||||
// ====================== 视图生命周期回调 ======================
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
// 控件从窗口移除时,从缓存中清除,避免内存泄漏
|
||||
sViewCache.remove(this);
|
||||
LogUtils.d(TAG, "onDetachedFromWindow: 控件实例已从缓存移除");
|
||||
LogUtils.d(TAG, "onDetachedFromWindow remove view from static cache");
|
||||
}
|
||||
|
||||
// ====================== 测量与绘制区 ======================
|
||||
// ====================== 测量绘制工具私有方法 ======================
|
||||
/**
|
||||
* 测量辅助函数
|
||||
* 测量尺寸计算工具
|
||||
* @param defaultSize 默认尺寸
|
||||
* @param measureSpec 测量规格
|
||||
* @return 最终计算尺寸
|
||||
*/
|
||||
private int measureSize(int defaultSize, int measureSpec) {
|
||||
private int measureSize(final int defaultSize, final int measureSpec) {
|
||||
int result = defaultSize;
|
||||
int specMode = MeasureSpec.getMode(measureSpec);
|
||||
int specSize = MeasureSpec.getSize(measureSpec);
|
||||
final int specMode = MeasureSpec.getMode(measureSpec);
|
||||
final int specSize = MeasureSpec.getSize(measureSpec);
|
||||
if (specMode == MeasureSpec.EXACTLY) {
|
||||
result = specSize;
|
||||
} else if (specMode == MeasureSpec.AT_MOST) {
|
||||
@@ -310,90 +293,83 @@ public class DunTemperatureView extends View {
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
// 强制控件整体左右内边距=0,彻底消除外部边距
|
||||
setPadding(0, getPaddingTop(), 0, getPaddingBottom());
|
||||
|
||||
// 控件宽度=温度条宽度 + 文本区宽度(均为5dp转px,无额外空白)
|
||||
int defaultWidth = mThermometerWidth + mTextAreaWidth;
|
||||
int width = measureSize(defaultWidth, widthMeasureSpec);
|
||||
int height = measureSize(DEFAULT_HEIGHT, heightMeasureSpec);
|
||||
final int defaultWidth = mThermometerWidth + mTextAreaWidth;
|
||||
final int width = measureSize(defaultWidth, widthMeasureSpec);
|
||||
final int height = measureSize(DEFAULT_HEIGHT, heightMeasureSpec);
|
||||
setMeasuredDimension(width, height);
|
||||
|
||||
// 根据文本位置配置,计算温度条矩形坐标
|
||||
float thermometerLeft, thermometerRight;
|
||||
if (isTextOnRight) {
|
||||
// 文本在右侧:温度条靠左,右接文本区
|
||||
float thermometerLeft;
|
||||
float thermometerRight;
|
||||
final boolean textRightFlag = isTextOnRight;
|
||||
if (textRightFlag) {
|
||||
thermometerLeft = 0;
|
||||
thermometerRight = thermometerLeft + mThermometerWidth;
|
||||
} else {
|
||||
// 文本在左侧:温度条靠右,左接文本区
|
||||
thermometerLeft = width - mThermometerWidth;
|
||||
thermometerRight = width;
|
||||
}
|
||||
float thermometerTop = getPaddingTop();
|
||||
float thermometerBottom = height - getPaddingBottom();
|
||||
final float thermometerTop = getPaddingTop();
|
||||
final float thermometerBottom = height - getPaddingBottom();
|
||||
mThermometerRect.set(thermometerLeft, thermometerTop, thermometerRight, thermometerBottom);
|
||||
|
||||
LogUtils.v(TAG, "onMeasure: 文本位置=" + (isTextOnRight ? "右侧" : "左侧") + ",控件尺寸=" + width + "x" + height + ",温度条区域=" + mThermometerRect.toShortString());
|
||||
LogUtils.v(TAG, "onMeasure textPos=" + (textRightFlag ? "RIGHT" : "LEFT") + ", view size=" + width + "x" + height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
protected void onDraw(final Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
// 1. 绘制温度条边框(5dp宽度,小圆角适配)
|
||||
// 绘制温度条外边框
|
||||
canvas.drawRoundRect(mThermometerRect, 3, 3, mThermometerPaint);
|
||||
|
||||
// 2. 计算填充高度(根据当前值占最大值的比例)
|
||||
float fillRatio = (float) mCurrentValue / mMaxValue;
|
||||
float fillHeight = mThermometerRect.height() * fillRatio;
|
||||
float fillTop = mThermometerRect.bottom - fillHeight;
|
||||
// 计算渐变填充区域高度比例
|
||||
final float fillRatio = (float) mCurrentValue / mMaxValue;
|
||||
final float fillHeight = mThermometerRect.height() * fillRatio;
|
||||
final float fillTop = mThermometerRect.bottom - fillHeight;
|
||||
|
||||
// 3. 绘制渐变填充部分(左右贴边框,无间距)
|
||||
// 绘制渐变填充色块
|
||||
if (fillHeight > 0) {
|
||||
RectF fillRect = new RectF(
|
||||
mThermometerRect.left + FILL_PADDING_HORIZONTAL,
|
||||
fillTop + FILL_PADDING_VERTICAL,
|
||||
mThermometerRect.right - FILL_PADDING_HORIZONTAL,
|
||||
mThermometerRect.bottom - FILL_PADDING_VERTICAL
|
||||
final RectF fillRect = new RectF(
|
||||
mThermometerRect.left + FILL_PADDING_HORIZONTAL,
|
||||
fillTop + FILL_PADDING_VERTICAL,
|
||||
mThermometerRect.right - FILL_PADDING_HORIZONTAL,
|
||||
mThermometerRect.bottom - FILL_PADDING_VERTICAL
|
||||
);
|
||||
LinearGradient gradient = new LinearGradient(
|
||||
fillRect.centerX(), fillRect.bottom,
|
||||
fillRect.centerX(), fillRect.top,
|
||||
mGradientColors,
|
||||
mGradientPositions,
|
||||
Shader.TileMode.CLAMP
|
||||
final LinearGradient gradient = new LinearGradient(
|
||||
fillRect.centerX(), fillRect.bottom,
|
||||
fillRect.centerX(), fillRect.top,
|
||||
mGradientColors,
|
||||
mGradientPositions,
|
||||
Shader.TileMode.CLAMP
|
||||
);
|
||||
mFillPaint.setShader(gradient);
|
||||
canvas.drawRoundRect(fillRect, 2, 2, mFillPaint);
|
||||
mFillPaint.setShader(null);
|
||||
}
|
||||
|
||||
// 4. 绘制文本(5dp固定宽度文本区,底部对齐竖排,文字居中)
|
||||
String text = String.format("%d/%d", mCurrentValue, mMaxValue);
|
||||
if (text.isEmpty()) return;
|
||||
// 竖排文本绘制逻辑
|
||||
final String text = String.format("%d/%d", mCurrentValue, mMaxValue);
|
||||
if (text.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
float textBaseX;
|
||||
if (isTextOnRight) {
|
||||
// 文本在右侧:X=温度条右边缘 + 文本区宽度的一半(紧贴温度条)
|
||||
final boolean textRightFlag = isTextOnRight;
|
||||
if (textRightFlag) {
|
||||
textBaseX = mThermometerRect.right + (mTextAreaWidth / 2f);
|
||||
} else {
|
||||
// 文本在左侧:X=文本区宽度的一半(紧贴控件左边缘)
|
||||
textBaseX = mTextAreaWidth / 2f;
|
||||
}
|
||||
|
||||
// 文本绘制参数(适配5dp窄区域,缩小字间距提升紧凑度)
|
||||
float singleCharHeight = mTextPaint.getTextSize() + (TEXT_CHAR_SPACING - 2f); // 字间距减为6f
|
||||
float totalTextHeight = (singleCharHeight * text.length()) - (TEXT_CHAR_SPACING - 2f);
|
||||
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
|
||||
float charBottomOffset = fontMetrics.bottom;
|
||||
final float singleCharHeight = mTextPaint.getTextSize() + (TEXT_CHAR_SPACING - 2f);
|
||||
final Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
|
||||
final float charBottomOffset = fontMetrics.bottom;
|
||||
final float startTextY = getHeight() - getPaddingBottom() - charBottomOffset;
|
||||
|
||||
// 文本起始Y(底部对齐控件底部,无间距)
|
||||
float startTextY = getHeight() - getPaddingBottom() - charBottomOffset;
|
||||
|
||||
// 逐字竖排绘制(文字居中于5dp文本区,无超出)
|
||||
for (int i = 0; i < text.length(); i++) {
|
||||
char singleChar = text.charAt(i);
|
||||
float currentTextY = startTextY - (i * singleCharHeight);
|
||||
final char singleChar = text.charAt(i);
|
||||
final float currentTextY = startTextY - (i * singleCharHeight);
|
||||
canvas.drawText(String.valueOf(singleChar), textBaseX, currentTextY, mTextPaint);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user