电池电量icon优化

This commit is contained in:
fengjignqi
2026-06-02 13:58:54 +08:00
parent b7ab2e69a4
commit 03e67932b1
49 changed files with 293 additions and 19 deletions

View File

@@ -0,0 +1,66 @@
package com.ckkj.water.utils.manager;
import android.content.Context;
import com.ckkj.water.R;
/**
* @author fengxiaoyang
* @date 2026/6/2
* @description 电池图标管理,根据电量值返回对应的图标资源
*/
public class PowerIconManage {
private static PowerIconManage instance;
private int[] horIconRes = {R.mipmap.hor_0, R.mipmap.hor_10, R.mipmap.hor_20, R.mipmap.hor_30,
R.mipmap.hor_40, R.mipmap.hor_50, R.mipmap.hor_60, R.mipmap.hor_70,
R.mipmap.hor_80, R.mipmap.hor_90, R.mipmap.hor_100};
private int [] verIconRes = {R.mipmap.ver_0, R.mipmap.ver_10, R.mipmap.ver_20, R.mipmap.ver_30,
R.mipmap.ver_40, R.mipmap.ver_50, R.mipmap.ver_60, R.mipmap.ver_70,
R.mipmap.ver_80, R.mipmap.ver_90, R.mipmap.ver_100};
public static PowerIconManage getInstance() {
if (instance == null) {
synchronized (PowerIconManage.class) {
if (instance == null) {
instance = new PowerIconManage();
}
}
}
return instance;
}
/**
* 根据电量值获取对应的电池图标
* @param power 电量值 (0-100)
* @return 电池图标资源ID
*/
public int getPowerIcon(int power) {
if (power <= 0) {
return horIconRes[0];
}
if (power >= 100) {
return horIconRes[10];
}
// 计算对应的数组索引电量1~10对应索引111~20对应索引2...
int index = (power + 9) / 10;
return horIconRes[index];
}
/**
* 根据电量值获取对应的电池图标
* @param power 电量值 (0-100)
* @return 电池图标资源ID
*/
public int getPowerVerIcon(int power) {
if (power <= 0) {
return verIconRes[0];
}
if (power >= 100) {
return verIconRes[10];
}
// 计算对应的数组索引电量1~10对应索引111~20对应索引2...
int index = (power + 9) / 10;
return verIconRes[index];
}
}