写个电量显示的玩意?
这个电量有什么要求呢?
- 充电的时候,会显示动画,除非满血100%
- 插上电源,显示插电状态图标
- 省电模式的时候,显示黄色进度
- 平时显示按电量梯度显示
基本结构
public class BatteryView extends View {
private int width;
private int height;
private int mCurrentLevel;
private Paint paint;
private int picWidth;
private int picHeight;
private BitmapFactory.Options options;
private Bitmap chargingPic;
private BatteryMode mCurrentMode = BatteryMode.NORMAL;
public BatteryView(Context context) {
this(context, null);
}
public BatteryView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BatteryView(Context context, @Nullable AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.mipmap.battery_level_save_100, options);
picWidth = bitmap.getWidth();
picHeight = bitmap.getHeight();
//加载静态图片列表
initResource();
chargingPic = BitmapFactory.decodeResource(getResources(),
R.mipmap.charge_state, options);
}
//充电状态图标变化
private final int[] chargeBatteryRes = new int[6];
//普通状态图标
private final int[] normalBatteryRes = new int[7];
//省电模式图标
private final int[] saveModeBatteryRes = new int[7];
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
ICLogger.d("picWidth == > " + picWidth + " picHeight == > " + picHeight);
setMeasuredDimension(picWidth, picHeight);
}
private int currentDrawPic = -1;
public enum BatteryMode {
SAVE, NORMAL
}
public void updateMode(BatteryMode mode) {
this.mCurrentMode = mode;
}
private void initResource() {
//充电变化的图标
chargeBatteryRes[0] = R.mipmap.battery_chage_0;
chargeBatteryRes[1] = R.mipmap.battery_level_20;
chargeBatteryRes[2] = R.mipmap.battery_level_40;
chargeBatteryRes[3] = R.mipmap.battery_level_60;
chargeBatteryRes[4] = R.mipmap.battery_level_80;
chargeBatteryRes[5] = R.mipmap.battery_level_100;
//普通状态图标
normalBatteryRes[0] = R.mipmap.battery_level_0;
normalBatteryRes[1] = R.mipmap.battery_level_10;
normalBatteryRes[2] = R.mipmap.battery_level_save_20;
normalBatteryRes[3] = R.mipmap.battery_level_40;
normalBatteryRes[4] = R.mipmap.battery_level_60;
normalBatteryRes[5] = R.mipmap.battery_level_80;
normalBatteryRes[6] = R.mipmap.battery_level_100;
//省电模式状态图标
saveModeBatteryRes[0] = R.mipmap.battery_level_0;
saveModeBatteryRes[1] = R.mipmap.battery_level_10;
saveModeBatteryRes[2] = R.mipmap.battery_level_save_20;
saveModeBatteryRes[3] = R.mipmap.battery_level_save_40;
saveModeBatteryRes[4] = R.mipmap.battery_level_save_60;
saveModeBatteryRes[5] = R.mipmap.battery_level_save_80;
saveModeBatteryRes[6] = R.mipmap.battery_level_save_100;
}
}
详情请看代码注释了
对外暴露的功能
- charging(boolean isCharging) 修改当前的状态,是否为充电状态--->控制充电状态图标的显示,以及动画播放控制
- updateCurrentLev更新当前电量
- updateMode(BatteryMode mode) 更新当前模式为省电模式还是普通模式,根据模式显示不同的套图
完整代码
public class BatteryView extends View {
private int width;
private int height;
private int mCurrentLevel;
private Paint paint;
private int picWidth;
private int picHeight;
private BitmapFactory.Options options;
private Bitmap chargingPic;
private BatteryMode mCurrentMode = BatteryMode.NORMAL;
public BatteryView(Context context) {
this(context, null);
}
public BatteryView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.battery_level_save_100, options);
picWidth = bitmap.getWidth();
picHeight = bitmap.getHeight();
//加载静态图片列表
initResource();
chargingPic = BitmapFactory.decodeResource(getResources(), R.mipmap.charge_state, options);
}
//充电状态图标变化
private final int[] chargeBatteryRes = new int[6];
//普通状态图标
private final int[] normalBatteryRes = new int[7];
//省电模式图标
private final int[] saveModeBatteryRes = new int[7];
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
private boolean isCharging = false;
public void charging(boolean isCharging) {
//充电状态,播放动画,显示充电图标
this.isCharging = isCharging;
//充电,并且不是100%的电量
if (isCharging && mCurrentLevel != 100) {
post(task);
} else {
//显示当前电量图标
showCurrentLev();
}
}
//更新当前电量图标
public void updateCurrentLev(int level) {
if (level > 100 || level < 0) {
throw new IllegalArgumentException("level range must in [0,100]");
}
this.mCurrentLevel = level;
showCurrentLev();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
ICLogger.d("picWidth == > " + picWidth + " picHeight == > " + picHeight);
setMeasuredDimension(picWidth, picHeight);
}
private int currentDrawPic = -1;
public enum BatteryMode {
SAVE, NORMAL
}
public void updateMode(BatteryMode mode) {
this.mCurrentMode = mode;
}
private void showCurrentLev() {
loopIndex = 0;
//停掉所有的动画
removeCallbacks(task);
//对当前电量进行转换
int index;
if (mCurrentLevel < 5) {
index = 0;
} else if (mCurrentLevel < 20) {
index = 1;
} else if (mCurrentLevel < 40) {
index = 2;
} else if (mCurrentLevel < 60) {
index = 3;
} else if (mCurrentLevel < 80) {
index = 4;
} else if (mCurrentLevel < 99) {
index = 5;
} else {
index = 6;
}
ICLogger.d("current index == > " + index);
//判断是不是省电模式
if (mCurrentMode == BatteryMode.NORMAL) {
currentDrawPic = normalBatteryRes[index];
} else {
currentDrawPic = saveModeBatteryRes[index];
}
invalidate();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (currentDrawPic == -1) {
return;
}
//绘制内容
//电量图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), currentDrawPic, options);
canvas.drawBitmap(bitmap, 0, 0, paint);
//是否要绘制充电中的图标
if (isCharging) {
int left = width / 2 - chargingPic.getWidth() / 2;
canvas.drawBitmap(chargingPic, left, 0, paint);
}
}
private int loopIndex = 0;
private final Runnable task = new Runnable() {
@Override
public void run() {
if (loopIndex >= chargeBatteryRes.length) {
loopIndex = 0;
}
//循环读取内容
currentDrawPic = chargeBatteryRes[loopIndex];
invalidate();
loopIndex++;
postDelayed(this, 1000);
}
};
private void initResource() {
//充电变化的图标
chargeBatteryRes[0] = R.mipmap.battery_chage_0;
chargeBatteryRes[1] = R.mipmap.battery_level_20;
chargeBatteryRes[2] = R.mipmap.battery_level_40;
chargeBatteryRes[3] = R.mipmap.battery_level_60;
chargeBatteryRes[4] = R.mipmap.battery_level_80;
chargeBatteryRes[5] = R.mipmap.battery_level_100;
//普通状态图标
normalBatteryRes[0] = R.mipmap.battery_level_0;
normalBatteryRes[1] = R.mipmap.battery_level_10;
normalBatteryRes[2] = R.mipmap.battery_level_save_20;
normalBatteryRes[3] = R.mipmap.battery_level_40;
normalBatteryRes[4] = R.mipmap.battery_level_60;
normalBatteryRes[5] = R.mipmap.battery_level_80;
normalBatteryRes[6] = R.mipmap.battery_level_100;
//省电模式状态图标
saveModeBatteryRes[0] = R.mipmap.battery_level_0;
saveModeBatteryRes[1] = R.mipmap.battery_level_10;
saveModeBatteryRes[2] = R.mipmap.battery_level_save_20;
saveModeBatteryRes[3] = R.mipmap.battery_level_save_40;
saveModeBatteryRes[4] = R.mipmap.battery_level_save_60;
saveModeBatteryRes[5] = R.mipmap.battery_level_save_80;
saveModeBatteryRes[6] = R.mipmap.battery_level_save_100;
}
}