-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBatteryView.java
More file actions
201 lines (173 loc) · 5.78 KB
/
BatteryView.java
File metadata and controls
201 lines (173 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.example.yzy.webviewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
/**
* Created by YangZhenYu on 2016/9/6.
*/
public class BatteryView extends View {
private Paint mTextPait;
private Paint mBatteryPait;
private Paint mPowerPaint;
private float mBatteryStroke = 4.0f;
private float raidus = 2.0f;
/**
* 电池参数
*
* @param context
*/
private float mBatteryHeight = 30.0f;// 电池高度
private float mBatteryWidth = 60.0f;// 电池的宽度
private float mCapWidth;//电池盖的宽度
private float mCapHeight;//电池盖的高度
private float mPowerHeight;
private float mPowerWidth;
private float mPower = 0.5f;//默认电量
/**
* 矩形
*/
private RectF mBatteryRectF;//电池
private RectF mCapRectF;//电池盖
private RectF mPowerRectF;//电量
private float warningValue =0.2f;//警戒值
/**
* 设置电量
*
* @param pPower
*/
public void setPower(float pPower) {
mPower = pPower;
if (pPower < 0) {
mPower = 0f;
}
if (pPower > 1f) {
mPower = 1f;
}
invalidate();
}
/**
* 设置电量低于百分之多少时颜色变化
* @param pWarningValue
*/
public void setWarningValue(float pWarningValue){
this.warningValue = pWarningValue;
postInvalidate();
}
/**
* 设置电池宽高
*
* @param pBatteryWidth
* @param pBatteryHeight
*/
public void setWidthHeight(float pBatteryWidth, float pBatteryHeight) {
mBatteryWidth = pBatteryWidth;
mBatteryHeight = pBatteryHeight;
}
public BatteryView(Context context, float pBatteryWidth, float pBatteryHeight) {
super(context);
mBatteryWidth = pBatteryWidth;
mBatteryHeight = pBatteryHeight;
initSize();
initView(context);
}
public BatteryView(Context context, AttributeSet attrs) {
super(context, attrs);
initSize();
initView(context);
}
private void initView(Context context) {
mTextPait = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPait.setTextSize(sp2px(context, 8));
mTextPait.setColor(0xff303030);
mTextPait.setAntiAlias(true);
/**
* 设置电池画笔
*/
mBatteryPait = new Paint();
mBatteryPait.setColor(0xff29b473);
mBatteryPait.setStrokeWidth(mBatteryStroke);
mBatteryPait.setStyle(Paint.Style.STROKE);
mBatteryPait.setAntiAlias(true);
/**
* 电量画笔
*/
mPowerPaint = new Paint();
mPowerPaint.setStyle(Paint.Style.FILL);
mPowerPaint.setStrokeWidth(mBatteryStroke);
mPowerPaint.setAntiAlias(true);
/**
* 设置电池矩形
* 偏移一个弧度单位,防止线粗细不一
*/
mBatteryRectF = new RectF(raidus, raidus, mBatteryWidth + raidus, mBatteryHeight + raidus);
/**
* 设置电池盖矩形
*/
mCapRectF = new RectF(mBatteryWidth + raidus, (mBatteryHeight - mCapHeight + raidus) / 2,
mCapWidth + mBatteryWidth + raidus, (mBatteryHeight + mCapHeight + raidus) / 2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
mBatteryWidth = 60f;
} else {
mBatteryWidth = MeasureSpec.getSize(widthMeasureSpec) - raidus * 2 - mCapWidth;
}
if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
mBatteryHeight = 30f;
} else {
mBatteryHeight = MeasureSpec.getSize(heightMeasureSpec) - raidus * 2;
}
initSize();
initView(getContext());
setMeasuredDimension((int)(mBatteryWidth+mCapWidth*2),(int)(mBatteryHeight));
}
private void initSize() {
mCapHeight = mBatteryHeight / 2;
mCapWidth = mBatteryWidth / 20;
mPowerHeight = mBatteryHeight - mBatteryStroke;
mPowerWidth = mBatteryWidth - 2 * mBatteryStroke;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mPower > warningValue) {
//电量大于20%显示绿色
mBatteryPait.setColor(0xff29b473);
mPowerPaint.setColor(0xff29b473);
mTextPait.setColor(0xff303030);
} else if (mPower < warningValue) {
//电量少于20%显示红色
mBatteryPait.setColor(0xfff90f0f);
mPowerPaint.setColor(0xfff90f0f);
mTextPait.setColor(0xfff90f0f);
}
/**
* 设置电量的矩形
*/
mPowerRectF = new RectF(mBatteryStroke + raidus, mBatteryStroke + raidus, mPower * mPowerWidth + mBatteryStroke + raidus, mPowerHeight + raidus);
canvas.drawRoundRect(mBatteryRectF, raidus, raidus, mBatteryPait);
canvas.drawRoundRect(mCapRectF, raidus, raidus, mBatteryPait);
canvas.drawRect(mPowerRectF, mPowerPaint);
String text = (int) (mPower * 100) + "%";
float stringWidth = mTextPait.measureText(text);
float x = (getWidth() - stringWidth) / 2;
float y = mBatteryHeight / 2 + mBatteryHeight / 4;
canvas.drawText(text, x, y, mTextPait);
}
/**
* sp转px
*
* @param context
* @param spVal
* @return
*/
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, context.getResources().getDisplayMetrics());
}
}