电池电量icon优化
This commit is contained in:
@@ -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对应索引1,11~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对应索引1,11~20对应索引2...
|
||||
int index = (power + 9) / 10;
|
||||
return verIconRes[index];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user