Files
Xuewang365OSLenovo/app/src/main/java/com/uiui/zyos/view/BatteryView.java
tongtongstudio d83c3b9291 version:1.9.9
fix:
update:优化指示器颜色,优化个人中心,主页增加WiFi电池信息
2024-10-24 17:46:02 +08:00

226 lines
9.1 KiB
Java

package com.uiui.zyos.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.uiui.zyos.R;
import com.uiui.zyos.utils.ScreenUtils;
/**
* @CreateDate:
* @Author:lp
* @Description:
*/
public class BatteryView extends View {
private static final float OUTLINE_THICKNESS = 3.0f;//电池框厚度
private static final float CAP_WIDTH = 4.0f;//电池盖宽度
private static float CAP_HEIGHT;//电池盖高度
private static final float CAP_SPACE = 1.0f;//电池盖和电池的间隔
private static final float CAP_CORNER_RADIUS = 4;//电池盖矩形圆角
private static final float GAP_OF_SHAPE_BODY = 4.0f;//电池体与外框之间的间隙
private static final float ROUND_CORNER_RADIUS = 6;
private static final float TEXT_SIZE = 11f;
private int FLASH_WIDTH;//闪电的高度和宽度
private int FLASH_HEIGHT;
private float fullPowerWidth; //满电量时电池体的宽度。
private int mBatteryLevel = 20;
private boolean mCharging = false;
private Paint batteryBodyPainter;
private Paint batteryHeadPainter;
private Paint mPowerPaint;//电量画笔
private Paint backgroundPaint;//白色背景
private Paint mTextPaint;//文字
private RectF outlineRect;//电池矩形
private RectF mCapRect;//电池盖矩形
private RectF batteryRect;//电量矩形
private RectF backgroundRect;//背景矩形
private Rect mTextRect;//文字
private int left;
private int top;
public BatteryView(Context context) {
this(context, null);
}
public BatteryView(Context context, AttributeSet attrs) {
super(context, attrs);
//电池外框
batteryBodyPainter = new Paint();
batteryBodyPainter.setColor(ColorUtils.getColor(R.color.battery_box));
batteryBodyPainter.setAntiAlias(true);
batteryBodyPainter.setStyle(Paint.Style.STROKE);
batteryBodyPainter.setStrokeWidth(OUTLINE_THICKNESS);
//电池盖
batteryHeadPainter = new Paint();
batteryHeadPainter.setColor(ColorUtils.getColor(R.color.battery_box));
batteryHeadPainter.setAntiAlias(true);
batteryHeadPainter.setStyle(Paint.Style.FILL);
//电量
mPowerPaint = new Paint();
mPowerPaint.setAntiAlias(true);
mPowerPaint.setColor(ColorUtils.getColor(R.color.battery_normal_power));
mPowerPaint.setStyle(Paint.Style.FILL);
backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(ColorUtils.getColor(R.color.battery_background));
backgroundPaint.setStyle(Paint.Style.FILL);
mTextPaint = new Paint();
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
mTextPaint.setTextSize(dip2px(context, TEXT_SIZE));//文字大小
mTextPaint.setColor(ColorUtils.getColor(R.color.battery_text));
mTextPaint.setAntiAlias(true);
outlineRect = new RectF();
outlineRect.left = OUTLINE_THICKNESS;
outlineRect.top = OUTLINE_THICKNESS;
mCapRect = new RectF();
batteryRect = new RectF();
backgroundRect = new RectF();
mTextRect = new Rect();
}
public void setBatteryLevel(int battery, boolean isChanrging) {
this.mBatteryLevel = battery > 100 ? 100 : Math.max(battery, 1);
// this.mBatteryLevel = battery;
this.mCharging = isChanrging;
requestLayout();
}
public int getBatteryLevel() {
return mBatteryLevel;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int specWidthSize = MeasureSpec.getSize(widthMeasureSpec);//宽
int specHeightSize = MeasureSpec.getSize(heightMeasureSpec);//高
Log.e("onMeasure", "specWidthSize: " + specWidthSize);
Log.e("onMeasure", "specHeightSize: " + specHeightSize);
Log.e("dip2px", "onMeasure: " + ScreenUtils.dip2px(mContext, OUTLINE_THICKNESS));
FLASH_HEIGHT = (int) (specHeightSize - OUTLINE_THICKNESS * 2);
FLASH_WIDTH = FLASH_HEIGHT;
outlineRect.left = OUTLINE_THICKNESS + FLASH_WIDTH;
//设置电池外框
outlineRect.right = specWidthSize - OUTLINE_THICKNESS - CAP_SPACE - CAP_WIDTH;
outlineRect.bottom = specHeightSize - OUTLINE_THICKNESS;
Log.e("onMeasure", "outlineRect.right: " + outlineRect.right);
Log.e("onMeasure", "outlineRect.bottom: " + outlineRect.bottom);
backgroundRect.left = outlineRect.left;
backgroundRect.top = outlineRect.top;
backgroundRect.bottom = outlineRect.bottom;
backgroundRect.right = outlineRect.right;
Log.e("onMeasure", "backgroundRect.left : " + backgroundRect.left);
Log.e("onMeasure", "backgroundRect.top : " + backgroundRect.top);
Log.e("onMeasure", "backgroundRect.bottom : " + backgroundRect.bottom);
Log.e("onMeasure", "backgroundRect.right : " + backgroundRect.right);
//设置电池盖矩形
CAP_HEIGHT = specHeightSize / 2;
mCapRect.left = outlineRect.right + OUTLINE_THICKNESS + CAP_SPACE;
mCapRect.top = (float) specHeightSize / 2 - CAP_HEIGHT / 2;
mCapRect.right = specWidthSize;
mCapRect.bottom = (float) specHeightSize / 2 + CAP_HEIGHT / 2;
Log.e("onMeasure", "mCapRect.left: " + mCapRect.left);
Log.e("onMeasure", "mCapRect.top: " + mCapRect.top);
Log.e("onMeasure", "mCapRect.right: " + mCapRect.right);
Log.e("onMeasure", "mCapRect.bottom: " + mCapRect.bottom);
//设置电池体
batteryRect.left = outlineRect.left + GAP_OF_SHAPE_BODY;
batteryRect.top = outlineRect.top + GAP_OF_SHAPE_BODY;
batteryRect.bottom = outlineRect.bottom - GAP_OF_SHAPE_BODY;
Log.e("onMeasure", "batteryRect.left: " + batteryRect.left);
Log.e("onMeasure", "batteryRect.top: " + batteryRect.top);
Log.e("onMeasure", "batteryRect.bottom: " + batteryRect.bottom);
//闪电位置
left = (int) (specWidthSize / 2 - OUTLINE_THICKNESS * 2);
top = (int) (specHeightSize / 2 - OUTLINE_THICKNESS * 2);
fullPowerWidth = outlineRect.right - GAP_OF_SHAPE_BODY - batteryRect.left;
setMeasuredDimension(specWidthSize, specHeightSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
batteryRect.right = (float) mBatteryLevel / 100 * fullPowerWidth + batteryRect.left;
Log.e("onMeasure", "batteryRect.right : " + batteryRect.right);
if (mCharging) {
mPowerPaint.setColor(ColorUtils.getColor(R.color.battery_charing));
} else {
if (mBatteryLevel <= 20) {
mPowerPaint.setColor(ColorUtils.getColor(R.color.battery_low_power));
} else {
mPowerPaint.setColor(ColorUtils.getColor(R.color.battery_normal_power));
}
}
canvas.drawRoundRect(backgroundRect, ROUND_CORNER_RADIUS, ROUND_CORNER_RADIUS, backgroundPaint);
canvas.drawRoundRect(outlineRect, ROUND_CORNER_RADIUS, ROUND_CORNER_RADIUS, batteryBodyPainter);
canvas.drawRoundRect(mCapRect, CAP_CORNER_RADIUS, CAP_CORNER_RADIUS, batteryHeadPainter);
canvas.drawRoundRect(batteryRect, ROUND_CORNER_RADIUS - 1, ROUND_CORNER_RADIUS - 1, mPowerPaint);
String level = String.valueOf(mBatteryLevel);
mTextPaint.getTextBounds(level, 0, level.length(), mTextRect);
Log.e("onMeasure", "mTextRect.right : " + mTextRect.right);
Log.e("onMeasure", "mTextRect.left : " + mTextRect.left);
Log.e("onMeasure", "mTextRect.top : " + mTextRect.top);
Log.e("onMeasure", "mTextRect.bottom : " + mTextRect.bottom);
int width1 = mTextRect.width();
int height1 = mTextRect.height();
Log.e("onMeasure", "width1 : " + width1);
Log.e("onMeasure", "height1 : " + height1);
// canvas.drawText(level, (batteryRect.right - batteryRect.left - width1) - mTextRect.left, (batteryRect.bottom - batteryRect.top - height1) - mTextRect.top, mTextPaint);
canvas.drawText(level, backgroundRect.left + (backgroundRect.width() - width1 - OUTLINE_THICKNESS) / 2, backgroundRect.bottom - (backgroundRect.height() - height1 + OUTLINE_THICKNESS) / 2, mTextPaint);
if (mCharging) {
Bitmap bitmap = ImageUtils.getBitmap(R.drawable.ic_battery_charging);
Bitmap bitmapScale = Bitmap.createScaledBitmap(bitmap, FLASH_WIDTH, FLASH_HEIGHT, true);
canvas.drawBitmap(bitmapScale, 0, (CAP_HEIGHT * 2 - FLASH_HEIGHT) / 2, null);
}
}
}