BatteryStateView.java
10.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package com.sw.laryngoscope.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import com.sw.laryngoscope.R;
import com.sw.laryngoscope.utils.Logger;
/**
* 电量显示控件
*/
public class BatteryStateView extends View {
private Paint mPaint;//文字的画笔
private Paint mBatteryPaint;//电池画笔
private Paint mCurPowerPaint;//电量画笔
private Paint mCapPowerPaint;//电量画笔
private float mBatteryStroke = 1;//电池框宽度
private RectF mBatteryRect;//电池矩形
private Rect mCapRect;//电池盖矩形
private RectF mPowerRect;//电量矩形
public static Typeface typeFace;
private int specWidthSize, specHeightSize;
private float textSize;
private int batteryColor;//电池框颜色
private int powerColor;//电量颜色
private int lowPowerColor;//低电颜色
private int noPowerColor;//低电颜色
private boolean isShowText;
private int lowerPower = 30;
private int noPower = 20;
private int power = 19;//当前电量(满电100)
private float textWidth = 0;//电量文字长度
private float mCapWidth;
private float mCapHeight = 12;
private boolean isCharge = false;
private boolean isWirelessCharge = false;
private Bitmap wireimg;
private Bitmap img;
public void setPower(int power) {
if ( power < 0 ) {
power = 0;
} else if ( power > 100) {
power = 100;
}
this.power = power;
invalidate();
}
public BatteryStateView(Context context) {
this(context, null);
}
public BatteryStateView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BatteryStateView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public BatteryStateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
//typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/" + Datas.logoFont);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.BatteryStateView);
textSize = typedArray.getDimension(R.styleable.BatteryStateView_batteryTextSize,22);//#0198BD 008AAC #51d2f2
batteryColor = typedArray.getColor(R.styleable.BatteryStateView_batteryColor,Color.argb(255, 77, 77, 77));
powerColor = typedArray.getColor(R.styleable.BatteryStateView_powerColor,Color.argb(255, 84, 189, 23));
lowPowerColor = typedArray.getColor(R.styleable.BatteryStateView_lowPowerColor,Color.argb(255, 255, 175, 77));
noPowerColor = typedArray.getColor(R.styleable.BatteryStateView_lowPowerColor,Color.argb(255, 255, 10, 10));
isShowText = typedArray.getBoolean(R.styleable.BatteryStateView_showText,false);
isShowText = false;
mCapWidth = typedArray.getDimension(R.styleable.BatteryStateView_mCapWidth,20);
typedArray.recycle();
initPaint();
}
public void initPaint() {
/**
* 设置电池画笔
*/
mBatteryPaint = new Paint();
mBatteryPaint.setColor(batteryColor);
mBatteryPaint.setAntiAlias(true);
//mBatteryPaint.setStyle(Paint.Style.STROKE);
mBatteryPaint.setStyle(Paint.Style.FILL);
//mBatteryPaint.setStrokeWidth(mBatteryStroke);
/**
* 设置电量画笔
*/
mCurPowerPaint = new Paint();
mCurPowerPaint.setAntiAlias(true);
mCurPowerPaint.setStyle(Paint.Style.FILL);
/**
* 设置电量画笔
*/
mCapPowerPaint = new Paint();
mCapPowerPaint.setAntiAlias(true);
mCapPowerPaint.setStyle(Paint.Style.FILL);
/**
* 设置文字画笔
*/
mPaint = new Paint();
mPaint.setAntiAlias(true);
textSize = 12;
mPaint.setTextSize(textSize);
//wireimg = BitmapFactory.decodeResource(getResources(), R.mipmap.img_charge);
Drawable drawable = getResources().getDrawable(R.mipmap.img_charge);
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(Color.parseColor("#E3E1E1"),
PorterDuff.Mode.SRC_ATOP);
drawable.setColorFilter(porterDuffColorFilter);
wireDrawableToBitamp(drawable);
Drawable drawable1 = getResources().getDrawable(R.mipmap.img_wireless_charging_fill_1);
PorterDuffColorFilter porterDuffColorFilter1 = new PorterDuffColorFilter(Color.parseColor("#E3E1E1"),
PorterDuff.Mode.SRC_ATOP);
drawable1.setColorFilter(porterDuffColorFilter1);
drawableToBitamp(drawable1);
//img = ((BitmapDrawable) drawable).getBitmap();
}
private void wireDrawableToBitamp(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
//System.out.println("Drawable转Bitmap");
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
w = 15;
h = 27;
wireimg = Bitmap.createBitmap(w, h, config);
//注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(wireimg);
drawable.setBounds(0, 0, w, h);
Logger.e("specWidthSize: w " + w + " h " + h);
drawable.draw(canvas);
}
private void drawableToBitamp(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
//System.out.println("Drawable转Bitmap");
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
w = 24;
h = 24;
img = Bitmap.createBitmap(w, h, config);
//注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(img);
drawable.setBounds(0, 0, w, h);
Logger.e("specWidthSize: w " + w + " h " + h);
drawable.draw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
specWidthSize = MeasureSpec.getSize(widthMeasureSpec);//宽
specHeightSize = MeasureSpec.getSize(heightMeasureSpec);//高
Logger.e("specWidthSize:" + specWidthSize);
Logger.e("specHeightSize:" + specHeightSize);
setMeasuredDimension(specWidthSize, specHeightSize);
specHeightSize -= 0;//10
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int blankSpace = 2;
if ( power <= noPower ) {
mCapPowerPaint.setColor(batteryColor);
mCurPowerPaint.setColor(noPowerColor);
mPaint.setColor(lowPowerColor);
} else if ( power <= lowerPower ) {
mCapPowerPaint.setColor(batteryColor);
mCurPowerPaint.setColor(lowPowerColor);
mPaint.setColor(lowPowerColor);
} else {
mCapPowerPaint.setColor(powerColor);
mCurPowerPaint.setColor(powerColor);
mPaint.setColor(powerColor);
}
if( isShowText ) {
String textString = power + "%";
Rect textRect = new Rect();
mPaint.getTextBounds(textString, 0, textString.length(), textRect);
textWidth = textRect.width();
float textHeight = textRect.height();
Logger.e("textWidth:" + textWidth + "textHeight:" + textHeight);
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
//canvas.drawText(textString, (specWidthSize - textWidth) / 2, specHeightSize + 3, mPaint);
} else {
textWidth = 0;
}
/**
* 设置电池矩形
* 2 间隔距离
*/
mBatteryRect = new RectF(0, 0, specWidthSize - mCapWidth - blankSpace, specHeightSize);
/**
* 设置电池盖矩形
*/
mCapRect = new Rect((int)(specWidthSize - mCapWidth), (int)(specHeightSize / 2 - mCapHeight / 2),
specWidthSize, (int)(specHeightSize / 2 + mCapHeight / 2));
/**
* 设置电量矩形
*/
float Width;
if ( power < 5 ) {
Width = ( specWidthSize - mCapWidth - blankSpace ) / 100.0f * 5;
} else {
Width = ( specWidthSize - mCapWidth - blankSpace ) / 100.0f * power;
}
mPowerRect = new RectF(0, 0, Width, specHeightSize);
canvas.drawRoundRect(mBatteryRect, 3, 3, mBatteryPaint);// 画电池背景
canvas.drawRect(mCapRect, mCapPowerPaint);// 画电池盖
canvas.drawRoundRect(mPowerRect, 3, 3, mCurPowerPaint);// 画电量
if ( isCharge ) {
try {
Paint tempPaint = new Paint();
canvas.drawBitmap(wireimg,
(specWidthSize - wireimg.getWidth()) / 2 - mCapWidth / 2,
(specHeightSize - wireimg.getHeight()) / 2, tempPaint);
} catch (Exception e) {
e.getStackTrace();
}
}
if ( isWirelessCharge ) {
try {
Paint tempPaint = new Paint();
canvas.drawBitmap(img,
(specWidthSize - img.getWidth()) / 2 - mCapWidth / 2,
(specHeightSize - img.getHeight()) / 2, tempPaint);
} catch (Exception e) {
e.getStackTrace();
}
}
}
public boolean isCharge() {
return isCharge;
}
public void setCharge(boolean charge) {
isCharge = charge;
}
public void setWirelessCharge(boolean charge) {
isWirelessCharge = charge;
}
}