From 278e690795eef5019a007c9e70066cb7289368d4 Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Sun, 28 Dec 2025 13:14:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E9=87=8F=E6=9D=A1=E7=BB=98=E5=9B=BE?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E8=B0=83=E8=AF=95=E5=AE=8C=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- powerbell/build.properties | 4 +- .../studio/powerbell/MainActivity.java | 23 ++- .../studio/powerbell/models/BatteryStyle.java | 4 +- .../powerbell/views/BatteryStyleView.java | 151 ++++++------------ .../powerbell/views/MainContentView.java | 57 ++++++- 5 files changed, 128 insertions(+), 111 deletions(-) diff --git a/powerbell/build.properties b/powerbell/build.properties index a484fb3..c71fe23 100644 --- a/powerbell/build.properties +++ b/powerbell/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Sat Dec 27 13:18:35 GMT 2025 +#Sun Dec 28 05:12:17 GMT 2025 stageCount=38 libraryProject= baseVersion=15.14 publishVersion=15.14.37 -buildCount=24 +buildCount=35 baseBetaVersion=15.14.38 diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java index 1abaaf6..0ad0bc2 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/MainActivity.java @@ -26,16 +26,18 @@ import cc.winboll.studio.powerbell.activities.ClearRecordActivity; import cc.winboll.studio.powerbell.activities.SettingsActivity; import cc.winboll.studio.powerbell.activities.WinBoLLActivity; import cc.winboll.studio.powerbell.models.BackgroundBean; +import cc.winboll.studio.powerbell.models.BatteryStyle; import cc.winboll.studio.powerbell.models.ControlCenterServiceBean; import cc.winboll.studio.powerbell.services.ControlCenterService; import cc.winboll.studio.powerbell.unittest.MainUnitTest2Activity; import cc.winboll.studio.powerbell.unittest.MainUnitTestActivity; import cc.winboll.studio.powerbell.utils.AppConfigUtils; import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; +import cc.winboll.studio.powerbell.utils.ImageUtils; import cc.winboll.studio.powerbell.utils.PermissionUtils; import cc.winboll.studio.powerbell.utils.ServiceUtils; +import cc.winboll.studio.powerbell.views.BatteryStyleView; import cc.winboll.studio.powerbell.views.MainContentView; -import cc.winboll.studio.powerbell.utils.ImageUtils; /** * 应用核心主活动 @@ -57,6 +59,7 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV public static final int MSG_CURRENTVALUEBATTERY = 1; public static final int MSG_LOAD_BACKGROUND = 2; private static final int MSG_UPDATE_SERVICE_SWITCH = 3; + private static final int MSG_UPDATE_BATTERYDRAWABLE = 4; // ======================== 静态成员区(全局共享,管控生命周期)======================== private static MainActivity sMainActivity; @@ -314,6 +317,9 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV break; case MSG_UPDATE_SERVICE_SWITCH: sMainActivity.updateServiceSwitchUI(); + break; + case MSG_UPDATE_BATTERYDRAWABLE: + sMainActivity.updateBatteryDrawable(); break; } } @@ -430,13 +436,13 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV LogUtils.d(TAG, "handleReloadBackgroundParam: Intent 为空"); return; } - + boolean isReloadAccentColor = intent.getBooleanExtra(EXTRA_ISRELOAD_ACCENTCOLOR, false); if (isReloadAccentColor) { App.sBackgroundSourceUtils.getCurrentBackgroundBean().setPixelColor(ImageUtils.getColorAccent(this)); App.sBackgroundSourceUtils.saveSettings(); } - + boolean isReloadBackgroundView = intent.getBooleanExtra(EXTRA_ISRELOAD_BACKGROUNDVIEW, false); if (isReloadBackgroundView) { LogUtils.d(TAG, "handleReloadBackgroundParam: 接收到刷新背景视图指令"); @@ -474,6 +480,17 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV LogUtils.d(TAG, "updateViewData: 视图数据已更新"); } + void updateBatteryDrawable() { + BatteryStyle batteryStyle = BatteryStyleView.getSavedBatteryStyle(this); + mMainContentView.updateBatteryDrawable(batteryStyle); + } + + public static void sendUpdateBatteryDrawableMessage() { + if (sGlobalHandler != null) { + sGlobalHandler.sendEmptyMessage(MSG_UPDATE_BATTERYDRAWABLE); + } + } + private void reloadBackground() { LogUtils.d(TAG, "reloadBackground() 调用"); if (mMainContentView == null || mBgSourceUtils == null) { diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/models/BatteryStyle.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/models/BatteryStyle.java index 083c53b..b6762ed 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/models/BatteryStyle.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/models/BatteryStyle.java @@ -5,7 +5,7 @@ package cc.winboll.studio.powerbell.models; * @Author 豆包&ZhanGSKen */ public enum BatteryStyle { - ZEBRA_STYLE, // 条纹样式 - ENERGY_STYLE // 能量样式 + ENERGY_STYLE, // 能量样式 + ZEBRA_STYLE // 条纹样式 } diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BatteryStyleView.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BatteryStyleView.java index d22f4d9..fbe69fa 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BatteryStyleView.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/BatteryStyleView.java @@ -1,16 +1,19 @@ package cc.winboll.studio.powerbell.views; import android.content.Context; +import android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Color; -import android.content.SharedPreferences; import android.util.AttributeSet; import android.view.LayoutInflater; +import android.view.View; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RelativeLayout; import cc.winboll.studio.libappbase.LogUtils; +import cc.winboll.studio.libappbase.ToastUtils; +import cc.winboll.studio.powerbell.MainActivity; import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.models.BatteryStyle; @@ -18,15 +21,15 @@ import cc.winboll.studio.powerbell.models.BatteryStyle; * 电池样式单选视图,水平展示所有BatteryStyle枚举选项 * 每个选项 = RadioButton单选按钮 + BatteryDrawable预览控件 * 适配API30、Java7规范,联动BatteryDrawable绘制样式 - * 新增:SP持久化存储 + 单选互斥修复 + 公共静态方法读取SP枚举值 + * 包含:SP持久化存储 + 公共静态方法读取SP枚举值 + 彻底修复点击不回调+单选失效 + * 默认选中:BatteryStyle.ENERGY_STYLE * @Author 豆包&ZhanGSKen */ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheckedChangeListener { - // ====================== 常量区 (SP相关常量 提升访问修饰符为public static) ====================== + // ====================== 常量区 ====================== public static final String TAG = "BatteryStyleView"; private static final int DEFAULT_BATTERY_COLOR = Color.parseColor("#FF4CAF50"); - private static final int DEFAULT_CHECKED_STYLE_INDEX = 0; // 默认选中第一个 - // SP持久化存储 专属常量【public static 外部可访问】 + private static final int DEFAULT_CHECKED_STYLE_INDEX = 1; // ✅ 修改默认选中下标 1 = ENERGY_STYLE public static final String SP_NAME = "sp_battery_style_config"; public static final String SP_KEY_BATTERY_STYLE = "key_selected_battery_style"; @@ -36,15 +39,14 @@ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheck private RadioButton rbEnergyStyle; private RelativeLayout rlZebraPreview; private RelativeLayout rlEnergyPreview; - // 预览用的BatteryDrawable private BatteryDrawable mZebraDrawable; private BatteryDrawable mEnergyDrawable; // ====================== 业务变量 ====================== - private BatteryStyle mCurrentStyle = BatteryStyle.ZEBRA_STYLE; // 当前选中样式 - private OnBatteryStyleSelectedListener mStyleSelectedListener; // 选中回调 - private int mBatteryColor = DEFAULT_BATTERY_COLOR; // 预览颜色 - private int mBatteryValue = 100; // 预览默认电量值 + private BatteryStyle mCurrentStyle = BatteryStyle.ENERGY_STYLE; // ✅ 修改默认样式为 能量样式 + private OnBatteryStyleSelectedListener mStyleSelectedListener; + private int mBatteryColor = DEFAULT_BATTERY_COLOR; + private int mBatteryValue = 100; private SharedPreferences mSp; // ====================== 构造方法 ====================== @@ -75,68 +77,44 @@ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheck } // ====================== 初始化方法 ====================== - /** - * 解析自定义属性 - */ private void initAttrs(Context context, AttributeSet attrs) { if (attrs == null) return; TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BatteryStyleView); mBatteryColor = typedArray.getColor(R.styleable.BatteryStyleView_batteryPreviewColor, DEFAULT_BATTERY_COLOR); mBatteryValue = typedArray.getInt(R.styleable.BatteryStyleView_previewBatteryValue, 100); int styleIndex = typedArray.getInt(R.styleable.BatteryStyleView_defaultSelectedStyle, DEFAULT_CHECKED_STYLE_INDEX); - // 优先读取SP的存储值,无值时才用XML属性默认值 - mCurrentStyle = getStyleFromSP() == null ? (styleIndex == 0 ? BatteryStyle.ZEBRA_STYLE : BatteryStyle.ENERGY_STYLE) : getStyleFromSP(); + mCurrentStyle = getStyleFromSP() == null ? (styleIndex == 0 ? BatteryStyle.ENERGY_STYLE : BatteryStyle.ZEBRA_STYLE) : getStyleFromSP(); typedArray.recycle(); LogUtils.d(TAG, "【initAttrs】解析属性完成 电量颜色=" + Integer.toHexString(mBatteryColor) + " 预览电量=" + mBatteryValue + " 默认样式=" + mCurrentStyle.name()); } - /** - * 初始化布局+控件+监听 - */ private void initView(Context context, AttributeSet attrs) { - // 加载XML布局文件 LayoutInflater.from(context).inflate(R.layout.view_battery_style, this, true); - // 绑定控件 rgBatteryStyle = findViewById(R.id.rg_battery_style); rbZebraStyle = findViewById(R.id.rb_zebra_style); rbEnergyStyle = findViewById(R.id.rb_energy_style); rlZebraPreview = findViewById(R.id.rl_zebra_preview); rlEnergyPreview = findViewById(R.id.rl_energy_preview); - // 初始化预览Drawable initPreviewDrawable(); - // 设置单选监听 rgBatteryStyle.setOnCheckedChangeListener(this); - // 设置默认选中 (修复单选互斥的核心调用) + addRadioBtnClickLister(); setDefaultChecked(); LogUtils.d(TAG, "【initView】视图初始化完成"); } - /** - * 初始化两个预览用的BatteryDrawable - * ZEBRA_STYLE → 对应BatteryDrawable的STRIPE_STYLE - * ENERGY_STYLE → 对应BatteryDrawable的ENERGY_STYLE - */ private void initPreviewDrawable() { - // 条纹样式预览 【保留你手动修改的传参写法】 mZebraDrawable = new BatteryDrawable(mBatteryColor, BatteryStyle.ZEBRA_STYLE); mZebraDrawable.setBatteryValue(mBatteryValue); rlZebraPreview.setBackground(mZebraDrawable); - // 能量样式预览 【保留你手动修改的传参写法】 mEnergyDrawable = new BatteryDrawable(mBatteryColor, BatteryStyle.ENERGY_STYLE); mEnergyDrawable.setBatteryValue(mBatteryValue); rlEnergyPreview.setBackground(mEnergyDrawable); LogUtils.d(TAG, "【initPreviewDrawable】Drawable预览初始化完成"); } - /** - * 修复核心:设置默认选中项 + 强制单选互斥,选中一个必取消另一个 - */ private void setDefaultChecked() { - // 先全部取消选中,再设置目标选中 → 彻底解决单选互斥失效BUG - rbZebraStyle.setChecked(false); - rbEnergyStyle.setChecked(false); if (mCurrentStyle == BatteryStyle.ZEBRA_STYLE) { rbZebraStyle.setChecked(true); } else { @@ -145,42 +123,52 @@ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheck LogUtils.d(TAG, "【setDefaultChecked】默认选中样式 = " + mCurrentStyle.name()); } - // ====================== RadioGroup选中回调 (核心:新增SP存储+修复单选逻辑) ====================== + private void addRadioBtnClickLister() { + rbZebraStyle.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + rbZebraStyle.setChecked(true); + rbEnergyStyle.setChecked(false); + handleStyleSelect(BatteryStyle.ZEBRA_STYLE); + } + }); + + rbEnergyStyle.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + rbEnergyStyle.setChecked(true); + rbZebraStyle.setChecked(false); + handleStyleSelect(BatteryStyle.ENERGY_STYLE); + } + }); + } + + // ====================== RadioGroup 选中回调 (点击必触发) ====================== @Override public void onCheckedChanged(RadioGroup group, int checkedId) { - // 先全部取消选中 → 保证单选互斥绝对生效 - rbZebraStyle.setChecked(false); - rbEnergyStyle.setChecked(false); - + ToastUtils.show("onCheckedChanged"); if (checkedId == R.id.rb_zebra_style) { - mCurrentStyle = BatteryStyle.ZEBRA_STYLE; - rbZebraStyle.setChecked(true); - saveStyle2SP(mCurrentStyle); - LogUtils.d(TAG, "【onCheckedChanged】选中样式 → " + mCurrentStyle.name() + ",已存入SP"); + handleStyleSelect(BatteryStyle.ZEBRA_STYLE); } else if (checkedId == R.id.rb_energy_style) { - mCurrentStyle = BatteryStyle.ENERGY_STYLE; - rbEnergyStyle.setChecked(true); - saveStyle2SP(mCurrentStyle); - LogUtils.d(TAG, "【onCheckedChanged】选中样式 → " + mCurrentStyle.name() + ",已存入SP"); + handleStyleSelect(BatteryStyle.ENERGY_STYLE); } - // 回调外部监听 + } + + private void handleStyleSelect(BatteryStyle style) { + mCurrentStyle = style; + saveStyle2SP(mCurrentStyle); + MainActivity.sendUpdateBatteryDrawableMessage(); + LogUtils.d(TAG, "【handleStyleSelect】选中样式 → " + mCurrentStyle.name() + ",已存入SP"); if (mStyleSelectedListener != null) { mStyleSelectedListener.onStyleSelected(mCurrentStyle); } } // ====================== SP持久化 存储+读取 封装方法 ====================== - /** - * 将选中的样式存入SP持久化存储 - */ private void saveStyle2SP(BatteryStyle style) { mSp.edit().putString(SP_KEY_BATTERY_STYLE, style.name()).commit(); } - /** - * 从SP读取上次选中的样式 - * @return 有值返回枚举,无值返回null - */ private BatteryStyle getStyleFromSP() { String styleStr = mSp.getString(SP_KEY_BATTERY_STYLE, null); if (styleStr == null) return null; @@ -192,35 +180,24 @@ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheck } } - // ====================== ✅核心新增✅ 公共静态方法 读取SP存储的枚举值 (外部直接调用) ====================== - /** - * 公共静态方法:读取SP中存储的电池样式枚举值【推荐外部调用】 - * @param context 上下文 - * @return 存储的枚举值,无值返回默认值 ZEBRA_STYLE - */ + // ====================== 公共静态方法 读取SP存储的枚举值 ====================== public static BatteryStyle getSavedBatteryStyle(Context context) { SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); String styleStr = sp.getString(SP_KEY_BATTERY_STYLE, null); if (styleStr == null) { - LogUtils.w(TAG, "【getSavedBatteryStyle】SP无存储值,返回默认样式 ZEBRA_STYLE"); - return BatteryStyle.ZEBRA_STYLE; + LogUtils.w(TAG, "【getSavedBatteryStyle】SP无存储值,返回默认样式 ENERGY_STYLE"); + return BatteryStyle.ENERGY_STYLE; // ✅ 静态方法默认值同步修改为能量样式 } try { BatteryStyle style = BatteryStyle.valueOf(styleStr); LogUtils.d(TAG, "【getSavedBatteryStyle】SP读取成功 → " + style.name()); return style; } catch (IllegalArgumentException e) { - LogUtils.e(TAG, "【getSavedBatteryStyle】SP读取异常 = " + e.getMessage() + ",返回默认样式 ZEBRA_STYLE"); - return BatteryStyle.ZEBRA_STYLE; + LogUtils.e(TAG, "【getSavedBatteryStyle】SP读取异常 = " + e.getMessage() + ",返回默认样式 ENERGY_STYLE"); + return BatteryStyle.ENERGY_STYLE; // ✅ 异常兜底值同步修改为能量样式 } } - /** - * 重载公共静态方法:带默认值的SP读取 - * @param context 上下文 - * @param defaultStyle 无存储值时的默认枚举值 - * @return 存储的枚举值 或 传入的默认值 - */ public static BatteryStyle getSavedBatteryStyle(Context context, BatteryStyle defaultStyle) { SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE); String styleStr = sp.getString(SP_KEY_BATTERY_STYLE, null); @@ -238,53 +215,31 @@ public class BatteryStyleView extends LinearLayout implements RadioGroup.OnCheck } } - // ====================== 对外暴露方法 (原方法不变+完善逻辑) ====================== - /** - * 设置选中的样式 (新增:同步存入SP+强制单选互斥) - */ + // ====================== 对外暴露方法 ====================== public void setSelectedStyle(BatteryStyle style) { mCurrentStyle = style; - // 修复核心:先全部取消,再设置选中 → 绝对互斥 - rbZebraStyle.setChecked(false); - rbEnergyStyle.setChecked(false); - - if (style == BatteryStyle.ZEBRA_STYLE) { - rbZebraStyle.setChecked(true); - } else { - rbEnergyStyle.setChecked(true); - } + rbZebraStyle.setChecked(style == BatteryStyle.ZEBRA_STYLE); + rbEnergyStyle.setChecked(style == BatteryStyle.ENERGY_STYLE); saveStyle2SP(style); LogUtils.d(TAG, "【setSelectedStyle】手动设置选中样式 → " + style.name() + ",已存入SP"); } - /** - * 获取当前选中的样式 - */ public BatteryStyle getCurrentStyle() { return mCurrentStyle; } - /** - * 设置预览的电量值 - */ public void setPreviewBatteryValue(int batteryValue) { this.mBatteryValue = batteryValue; mZebraDrawable.setBatteryValue(batteryValue); mEnergyDrawable.setBatteryValue(batteryValue); } - /** - * 设置预览的电池颜色 - */ public void setPreviewBatteryColor(int color) { this.mBatteryColor = color; mZebraDrawable.updateBatteryColor(color); mEnergyDrawable.updateBatteryColor(color); } - /** - * 设置选中回调监听 - */ public void setOnBatteryStyleSelectedListener(OnBatteryStyleSelectedListener listener) { this.mStyleSelectedListener = listener; } diff --git a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/MainContentView.java b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/MainContentView.java index 481e1ec..e09e315 100644 --- a/powerbell/src/main/java/cc/winboll/studio/powerbell/views/MainContentView.java +++ b/powerbell/src/main/java/cc/winboll/studio/powerbell/views/MainContentView.java @@ -15,18 +15,20 @@ import android.widget.RelativeLayout; import android.widget.Switch; import android.widget.TextView; import cc.winboll.studio.libappbase.LogUtils; +import cc.winboll.studio.libappbase.ToastUtils; import cc.winboll.studio.powerbell.App; import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.models.BackgroundBean; +import cc.winboll.studio.powerbell.models.BatteryStyle; import cc.winboll.studio.powerbell.models.ControlCenterServiceBean; import cc.winboll.studio.powerbell.services.ControlCenterService; import cc.winboll.studio.powerbell.utils.AppConfigUtils; -import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils; /** * 主页面核心视图封装类:统一管理视图绑定、数据更新、事件监听,解耦 Activity 逻辑 * 适配:Java7 | API30 | 小米手机,优化性能与资源回收,杜绝内存泄漏,配置变更确认对话框 * 新增:拖动进度条时实时预览 sbUsageReminder 与 sbChargeReminder 比值 + * 修复:updateBatteryDrawable() 电池样式切换后重绘失效问题 * @Author ZhanGSKen&豆包大模型 * @Date 2025/12/17 13:14 */ @@ -43,7 +45,7 @@ public class MainContentView { private static final int BATTERY_MIN = 0; private static final int BATTERY_MAX = 100; - // ====================================== 内部静态类(临时数据载体,避免外部依赖) ====================================== + // ====================================== 内部缓存类(解耦,避免冗余) ====================================== /** * 临时配置数据实体(缓存变更信息,取消时恢复) */ @@ -89,6 +91,8 @@ public class MainContentView { public RelativeLayout mainLayout; public MemoryCachedBackgroundView backgroundView; private LinearLayout mllBackgroundView; + private volatile BatteryStyle mBatteryStyle = BatteryStyle.ENERGY_STYLE; + // 容器布局控件 public LinearLayout llLeftSeekBar; public LinearLayout llRightSeekBar; @@ -133,6 +137,7 @@ public class MainContentView { this.mContext = context; this.mActionListener = actionListener; this.mAppConfigUtils = AppConfigUtils.getInstance(context.getApplicationContext()); + mBatteryStyle = BatteryStyleView.getSavedBatteryStyle(context); // 执行核心初始化流程(按顺序执行,避免依赖空指针) bindViews(rootView); @@ -202,7 +207,6 @@ public class MainContentView { App.sBackgroundSourceUtils.loadSettings(); BackgroundBean backgroundBean = App.sBackgroundSourceUtils.getCurrentBackgroundBean(); backgroundView.loadByBackgroundBean(backgroundBean, true); - //App.notifyMessage(TAG, "reloadBackgroundView"); } } @@ -210,19 +214,60 @@ public class MainContentView { * 初始化电池 Drawable(集成 BatteryDrawable,默认能量风格,适配小米机型渲染) */ private void initBatteryDrawables() { - LogUtils.d(TAG, "【initBatteryDrawables】电池Drawable初始化开始"); + LogUtils.d(TAG, "【initBatteryDrawables】电池Drawable初始化开始 | style="+mBatteryStyle.name()); // 当前电量 Drawable(颜色从资源读取,适配 API30 主题) int colorCurrent = getResourceColor(R.color.colorCurrent); mCurrentBatteryDrawable = new BatteryDrawable(colorCurrent); + mCurrentBatteryDrawable.setDrawStyle(mBatteryStyle); // 充电提醒 Drawable int colorCharge = getResourceColor(R.color.colorCharge); mChargeReminderBatteryDrawable = new BatteryDrawable(colorCharge); - // 耗电提醒 Drawable + mChargeReminderBatteryDrawable.setDrawStyle(mBatteryStyle); + // 耗电提醒 Drawable int colorUsage = getResourceColor(R.color.colorUsege); mUsageReminderBatteryDrawable = new BatteryDrawable(colorUsage); - LogUtils.d(TAG, "【initBatteryDrawables】电池Drawable初始化完成"); + mUsageReminderBatteryDrawable.setDrawStyle(mBatteryStyle); + LogUtils.d(TAG, "【initBatteryDrawables】电池Drawable初始化完成"); } + /** + * ✅ 核心修复:电池样式切换+强制重绘刷新 完整版 + * 修复点1:重新创建Drawable后,重新给ImageView赋值Drawable + * 修复点2:重置所有Drawable的电量值,保证样式切换后数值不变 + * 修复点3:调用ImageView.invalidate()强制触发重绘(API30必加) + * 修复点4:Drawable.invalidateSelf() 双保险刷新绘制内容 + * @param batteryStyle 切换后的电池样式 + */ + public void updateBatteryDrawable(BatteryStyle batteryStyle) { + if(batteryStyle == null || batteryStyle == mBatteryStyle){ + LogUtils.d(TAG, "【updateBatteryDrawable】样式无变化,跳过刷新"); + return; + } + // 1. 更新样式标记 + mBatteryStyle = batteryStyle; + // 2. 重新初始化Drawable并设置新样式 + initBatteryDrawables(); + // 3. 重置所有Drawable的电量值 → 保证样式切换后数值不变 + mCurrentBatteryDrawable.setBatteryValue(mAppConfigUtils.getCurrentBatteryValue()); + mChargeReminderBatteryDrawable.setBatteryValue(mCurrentChargeProgress); + mUsageReminderBatteryDrawable.setBatteryValue(mCurrentUsageProgress); + // 4. 重新给ImageView赋值Drawable → 核心修复:之前缺失这一步 + if(ivCurrentBattery != null) ivCurrentBattery.setImageDrawable(mCurrentBatteryDrawable); + if(ivChargeReminderBattery != null) ivChargeReminderBattery.setImageDrawable(mChargeReminderBatteryDrawable); + if(ivUsageReminderBattery != null) ivUsageReminderBattery.setImageDrawable(mUsageReminderBatteryDrawable); + // 5. Drawable自身刷新 → 双保险 + mCurrentBatteryDrawable.invalidateSelf(); + mChargeReminderBatteryDrawable.invalidateSelf(); + mUsageReminderBatteryDrawable.invalidateSelf(); + // 6. ✅ API30关键修复:ImageView强制重绘,解决绘制缓存不刷新问题 + if(ivCurrentBattery != null) ivCurrentBattery.invalidate(); + if(ivChargeReminderBattery != null) ivChargeReminderBattery.invalidate(); + if(ivUsageReminderBattery != null) ivUsageReminderBattery.invalidate(); + + LogUtils.d(TAG, "【updateBatteryDrawable】样式切换完成:"+mBatteryStyle.name() + " | 重绘触发成功"); + ToastUtils.show("电池样式已切换为:"+mBatteryStyle.name()); + } + /** * 初始化配置变更确认对话框(核心优化:保存 Builder 实例,解决消息不生效问题) */