package android.app;
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.Rect;
import android.graphics.RectF;
import android.os.BatteryManager;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class BatteryView extends View {
private int mMargin = 3; //电池内芯与边框的距离
private int mBoder = 2; //电池外框的宽带
private int mWidth = 45; //总长
private int mHeight = 23; //总高
private int mHeadWidth = 5;
private int mHeadHeight = 7;
private RectF mMainRect;
private RectF mHeadRect;
private float mRadius = 8.5f; //圆角
private float mPower;
private int count = 0;
private boolean mIsCharging; //是否在充电
private PowerConnectionReceiver mReceiver;
private Handler handler = new Handler();
public BatteryView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
public BatteryView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public BatteryView(Context context) {
super(context);
initView();
}
private void initView() {
//mHeadRect = new RectF(0, (mHeight - mHeadHeight) / 2, mHeadWidth, (mHeight + mHeadHeight) / 2);
mHeadRect = new RectF(mWidth - mBoder , (mHeight - mHeadHeight) / 2, mWidth - mBoder + mHeadWidth , (mHeight + mHeadHeight) / 2);
// float left = mHeadRect.width();
// float top = mBoder;
// float right = mWidth - mBoder;
// float bottom = mHeight - mBoder;
float left = mBoder;
float top = mBoder;
float right = mWidth - mBoder;
float bottom = mHeight - mBoder;
mMainRect = new RectF(left, top, right, bottom);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
count += 2;
if (count > 10) {
count = 0;
}
setPower((float) (count * 0.1));
handler.postDelayed(this, 600);
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint1 = new Paint();
//画电池头
paint1.setStyle(Paint.Style.FILL); //实心
paint1.setColor(Color.WHITE);
//canvas.drawRoundRect(mHeadRect, 0, 0, paint1);
Paint paint2 = new Paint();
//画外框
paint2.setStyle(Paint.Style.STROKE); //设置空心矩形
paint2.setStrokeWidth(mBoder); //设置边框宽度
paint2.setColor(Color.WHITE);
paint2.setAntiAlias(true);
canvas.drawRoundRect(mMainRect, mRadius, mRadius, paint2);
//画电池芯
// Paint paint = new Paint();
// if (mIsCharging) {
// paint.setColor(Color.GREEN);
// } else {
// if (mPower < 0.2) {
// paint.setColor(Color.RED);
// } else {
// paint.setColor(Color.GREEN);
// }
// }
int width = (int) (mPower * (mMainRect.width() - mMargin * 2));
// int left = (int) (mMainRect.right - mMargin - width);
// int right = (int) (mMainRect.right - mMargin);
// int top = (int) (mMainRect.top + mMargin);
// int bottom = (int) (mMainRect.bottom - mMargin);
int left = (int) (mMainRect.left + mMargin + width);
int right = (int) (mMainRect.left + mMargin);
int top = (int) (mMainRect.top + mMargin);
int bottom = (int) (mMainRect.bottom - mMargin);
// RectF rect = new RectF(left, top, right, bottom);
// paint.setAntiAlias(true);
// canvas.drawRect(rect, paint);
Paint mPaintBg = new Paint();
if (mIsCharging) {
mPaintBg.setColor(Color.GREEN);
} else {
if (mPower < 0.2) {
mPaintBg.setColor(Color.RED);
} else {
mPaintBg.setColor(Color.GREEN);
}
}
mPaintBg.setStyle(Paint.Style.FILL);
mPaintBg.setAntiAlias(true);
RectF rectfBg;
Path path = new Path();
rectfBg = new RectF(left, top, right, bottom);
if (mPower <= 0.1) {
path.addRoundRect(rectfBg, new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius}, Path.Direction.CW);
} else if ((mPower >= 0.9)) {
path.addRoundRect(rectfBg, new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius}, Path.Direction.CW);
} else {
path.addRoundRect(rectfBg, new float[]{mRadius, mRadius, 0, 0, 0, 0, mRadius, mRadius}, Path.Direction.CW);
}
canvas.drawPath(path, mPaintBg);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(mWidth + mHeadWidth + 2 , mHeight);
}
private void setPower(float power) {
mPower = power;
invalidate();
}
class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
mIsCharging = (usbCharge || acCharge);
if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {
mIsCharging = true;
} else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {
mIsCharging = false;
}
if (runnable == null || scale == 0) {
Log.d("zhj", "setPower default value 50% ");
setPower(0.5f);
return;
}
float powerValue = ((float) level) / scale;
BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
if (batteryManager != null)
powerValue = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) * 0.01f;
//Log.d("zhj", "Power value === " + powerValue);
if (mIsCharging && powerValue != 1.0) {
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 600);
} else {
handler.removeCallbacks(runnable);
setPower(powerValue);
}
}
}
;
@Override
protected void onAttachedToWindow() {
mReceiver = new PowerConnectionReceiver();
IntentFilter powerValueFilter = new IntentFilter();
powerValueFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
powerValueFilter.addAction(Intent.ACTION_POWER_CONNECTED);
powerValueFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
getContext().registerReceiver(mReceiver, powerValueFilter);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
getContext().unregisterReceiver(mReceiver);
count = 0;
super.onDetachedFromWindow();
}
}
转载请标明出处:https://blog.csdn.net/hj_key/article/details/103453741