源码整理

This commit is contained in:
2026-07-21 11:03:50 +08:00
parent 4247934a20
commit b1be995092
2 changed files with 126 additions and 194 deletions
+6 -6
View File
@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Mon Jul 20 14:47:50 CST 2026
stageCount=12
#Tue Jul 21 02:54:31 GMT 2026
stageCount=0
libraryProject=
baseVersion=15.20
publishVersion=15.20.11
buildCount=17
baseBetaVersion=15.20.12
baseVersion=15.21
publishVersion=15.21.0
buildCount=1
baseBetaVersion=15.21.1
@@ -13,79 +13,72 @@ 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
* @Describe 云盾华氏度热力视图,垂直盾值温度视图控件(带颜色渐变+静态Handler更新)
* 采用绘图方式展示盾值温度,填充色随盾值比例渐变,支持设置文本在温度条左侧/右侧,底部对齐竖排显示
* 温度条宽度=5dp,文本区宽度固定=5dp,整体左右边距=0,无任何多余空白间距
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/19 14:04:20 (香港时区时间)
* @lastEdit 2026/07/21 11:01:00 (香港时区时间)
* @Describe 云盾华氏度热力垂直视图控件,采用原生绘图实现盾值渐变温度条
* 1. 温度填充色随盾值比例渐变,支持左右切换竖排数值文本
* 2. 固定尺寸:温度条5dp、文本区域5dp,控件左右无外边距
* 3. 静态全局Handler统一更新所有控件实例,WeakHashMap防止内存泄漏
* 4. 全代码兼容Java7,无高版本语法特性,无try-with-resources
*/
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;
// 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 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.GREEN, Color.YELLOW, Color.RED};
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.GREEN, Color.YELLOW, Color.RED};
private float[] mGradientPositions = new float[]{0.0f, 0.5f, 1.0f};
private boolean isTextOnRight = true;
// 其他颜色配置
private int mBorderColor = Color.parseColor("#FF444444");
private int mTextColor = Color.parseColor("#FF000000");
// ====================== 静态代码块 ======================
static {
// 初始化静态Handler,绑定主线程Looper
sStaticHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(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 what = msg.what;
if (what == 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 msg, 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);
}
}
}
@@ -109,188 +102,134 @@ public class DunTemperatureView extends View {
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, context=" + context);
mThermometerWidth = dp2px(context, THERMOMETER_WIDTH_DP);
mTextAreaWidth = dp2px(context, TEXT_AREA_WIDTH_DP);
LogUtils.d(TAG, "init convert dp to px, 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 instance to cache");
}
// ====================== 工具方法区 ======================
/**
* dp 转 px(适配不同屏幕分辨率
* dp单位转换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 scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
// ====================== 对外控制方法 ======================
/**
* 设置文本相对于温度条的位置
* @param isOnRight true=文本在温度条右侧(默认),false=文本在温度条左侧
*/
public void setTextPosition(boolean isOnRight) {
// ====================== 对外UI配置方法 ======================
public void setTextPosition(final boolean isOnRight) {
this.isTextOnRight = isOnRight;
invalidate(); // 刷新布局绘制
LogUtils.d(TAG, "setTextPosition, isOnRight=" + isOnRight);
invalidate();
}
/**
* 获取当前文本位置配置
* @return true=右侧,false=左侧
*/
public boolean isTextOnRight() {
return isTextOnRight;
}
// ====================== 对外静态方法区 ======================
/**
* 静态外部方法:发送消息更新所有 DunTemperatureView 实例的盾值
* 可在子线程中调用
* @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 not initialized");
return;
}
// 封装参数到消息
Message msg = sStaticHandler.obtainMessage(MSG_UPDATE_DUN_VALUE);
LogUtils.d(TAG, "updateDunValue static call, 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
*/
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 value, input=" + 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
*/
public void setCurrentValue(int currentValue) {
int oldValue = this.mCurrentValue;
public void setCurrentValue(final int currentValue) {
final int oldValue = this.mCurrentValue;
this.mCurrentValue = Math.max(0, Math.min(currentValue, mMaxValue));
if (oldValue != this.mCurrentValue) {
LogUtils.d(TAG, "setCurrentValue: 当前盾值从" + oldValue + "更新为" + mCurrentValue);
invalidate(); // 重绘控件
LogUtils.d(TAG, "setCurrentValue change from " + oldValue + " to " + mCurrentValue);
invalidate();
}
}
/**
* 获取当前盾值
*/
public int getCurrentValue() {
return mCurrentValue;
}
/**
* 获取最高盾值
*/
public int getMaxValue() {
return mMaxValue;
}
/**
* 设置自定义渐变颜色
* @param colors 渐变颜色数组(至少2种颜色)
* @param positions 颜色位置数组(与colors长度一致,0.0~1.0
*/
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 color set success");
invalidate();
}
/**
* 设置温度条边框颜色
*/
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();
}
/**
* 设置文本颜色
*/
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 cache");
}
// ====================== 测量绘制 ======================
/**
* 测量辅助函数
*/
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) {
@@ -302,53 +241,50 @@ 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 textPosition=" + (textRightFlag ? "RIGHT" : "LEFT") + ", viewSize=" + width + "x" + height);
}
@Override
protected void onDraw(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(
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(
final LinearGradient gradient = new LinearGradient(
fillRect.centerX(), fillRect.bottom,
fillRect.centerX(), fillRect.top,
mGradientColors,
@@ -360,32 +296,28 @@ public class DunTemperatureView extends View {
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);
}
}