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