自定义电池view,仿华为视频电池样式,包含充电和不充电两种样式
package com.baina.batterydemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
/**
* @ClassName BatteryView2
* @Description 自定义电池图标
* @Author {Bai Jiansong}
* @Date 2022/1/15 20:44
* @Version 1.0
*/
public class BatteryView2 extends View {
private static final String TAG = "JPush";
// 电池内芯与边框的距离
private int margin = 2;
// 电池外框的宽度
private int border = 3;
// 电池总长
private int width = 0;
// 电池总高
private int height = 0;
// 电池头部宽
private int headWidth = 5;
// 电池头部高
private int headHeight = 0;
// 此边框矩形大小:view的大小 - 画笔宽的一半
private RectF outerRect;
private RectF headRect;
private RectF innerRect;
// 圆角半径
private float radius = 5f;
// 头部圆角半径
private float headRadius = 2f;
// 当前电量百分比
private float currentPower;
// 闪电图标路径
private Path path = new Path();
private Paint paintOuter = new Paint();
private Paint paintInner = new Paint();
private Paint paintLight = new Paint();
private BatteryBroadcastReceiver batteryBroadcastReceiver;
// 是否在充电,true - 充电
private boolean isCharging = false;
public BatteryView2(Context context) {
this(context, null);
}
public BatteryView2(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BatteryView2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
// 先画外框
paintOuter.setColor(Color.WHITE);
paintOuter.setAntiAlias(true);
paintOuter.setStyle(Paint.Style.STROKE);
paintOuter.setStrokeWidth(border);
// 画电量
paintInner.setStyle(Paint.Style.FILL);
// 充电的闪电图标
paintLight = new Paint();
paintLight.setColor(Color.WHITE);
paintLight.setStyle(Paint.Style.FILL_AND_STROKE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom() - getPaddingTop();
headHeight = height / 2;
createPath();
Log.i(TAG, "onMeasure: width:" + width);
Log.i(TAG, "onMeasure: height:" + height);
Log.i(TAG, "onMeasure: headHeight:" + headHeight);
}
private void createPath() {
// 创建闪电的路径
int mainWidth = width - headWidth;
path.moveTo(mainWidth * 4f / 7, height * 1f / 4);
path.lineTo(mainWidth * 13f / 35, height * 11f / 21);
path.lineTo(mainWidth * 17f / 35, height * 11f / 21);
path.lineTo(mainWidth * 3f / 7, height * 3f / 4);
path.lineTo(mainWidth * 22f / 35, height * 10f / 21);
path.lineTo(mainWidth * 18f / 35, height * 10f / 21);
path.close();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画外框
if (outerRect == null) {
float outLeft = (float) border / 2;
float outTop = (float) border / 2;
float outRight = width - headWidth - (float) border / 2;
float outBottom = height - (float) border / 2;
outerRect = new RectF(outLeft, outTop, outRight, outBottom);
}
canvas.drawRoundRect(outerRect, radius, radius, paintOuter);
// 画头部
if (headRect == null) {
int h_left = width - headWidth;
int h_top = height / 2 - headHeight / 2;
int h_right = h_left + headWidth;
int h_bottom = h_top + headHeight;
headRect = new RectF(h_left, h_top, h_right, h_bottom);
}
paintInner.setColor(Color.WHITE);
canvas.drawRoundRect(headRect, headRadius, headRadius, paintInner);
// 画电量
int p_left = border + margin;
int p_top = border + margin;
int p_right = p_left + (int) ((width - headWidth - border * 2 - margin * 2) * currentPower);
int p_bottom = height - margin - border;
if (innerRect == null) {
innerRect = new RectF(p_left, p_top, p_right, p_bottom);
} else {
innerRect.set(p_left, p_top, p_right, p_bottom);
}
if (isCharging) {
paintInner.setColor(Color.GREEN);
canvas.drawRect(innerRect, paintInner);
// 绘制闪电
canvas.drawPath(path, paintLight);
} else {
paintInner.setColor(Color.WHITE);
canvas.drawRect(innerRect, paintInner);
}
}
private void setPower(float power) {
// 刷新电量
this.currentPower = power;
if(currentPower < 0) {
currentPower = 0;
}
invalidate();
}
/**
* 注册电量变化广播
*/
public void registerBatteryReceiver() {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Context context = getContext();
if (context == null) {
Log.i(TAG, "registerBatteryReceiver: context is null");
return;
}
batteryBroadcastReceiver = new BatteryBroadcastReceiver();
context.registerReceiver(batteryBroadcastReceiver, intentFilter);
}
/**
* 取消注册电池电量广播
*/
public void unregisterBatteryReceiver() {
Context context = getContext();
if (context == null) {
Log.i(TAG, "registerBatteryReceiver: context is null");
return;
}
if (batteryBroadcastReceiver == null) {
return;
}
context.unregisterReceiver(batteryBroadcastReceiver);
}
/**
* 电池电量广播
*/
private class BatteryBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action == null) {
Log.e(TAG, "powerConnectionReceiver action is null");
return;
}
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
// 得到系统当前电量
int level = intent.getIntExtra("level", 0);
// 取得系统总电量
int fullPower = 100;
int total = intent.getIntExtra("scale", fullPower);
int defaultValue = -1;
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, defaultValue);
isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
Log.i(TAG, "onReceive: isCharging:" + isCharging);
Log.i(TAG, "onReceive: level:" + level);
Log.i(TAG, "onReceive: total:" + total);
setPower(((float) level) / total);
}
}
}
;
}
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private BatteryView2 batteryView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
batteryView2 = findViewById(R.id.battery_view2);
batteryView2.registerBatteryReceiver();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (batteryView2 != null) {
batteryView2.unregisterBatteryReceiver();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#616161"
android:orientation="vertical">
<LinearLayout
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.baina.batterydemo.BatteryView2
android:id="@+id/battery_view2"
android:layout_width="25dp"
android:layout_height="15dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"/>
</LinearLayout>
</LinearLayout>