├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── oyp
│ │ └── battery
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── oyp
│ │ │ └── battery
│ │ │ ├── BaseHandlerCallBack.java
│ │ │ ├── MainActivity.java
│ │ │ └── PowerConsumptionRankingsBatteryView.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── attrs.xml
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── oyp
│ └── battery
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── screenshot
├── v1.png
├── v2.png
├── v3.png
└── v4.mp4
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PowerConsumptionRankingsBatteryView
2 | 自定义一个电池View
3 |
4 | # 一、背景
5 | 最近公司有个业务,需要自定义一个电池控件,因此自己按照UI图编写了一个自定义View。
6 |
7 | # 二、效果
8 |
9 | 
10 |
11 | 
12 |
13 | 
14 |
15 | 
16 |
17 |
18 | # 三、实现过程
19 |
20 | 首先看下视觉给出的UI效果图
21 |
22 | 
23 |
24 | 从这里我们可以看得出来,要自定义这个电池View的话,分为3部分来绘制。
25 |
26 | 1. 第一部分是电池头
27 | 2. 第二部分是电池边框
28 | 3. 第三部分是电池电量
29 |
30 | ## 3.1 自定义属性
31 | 因此按照上面的UI效果图来做的话,我们自定义几个属性。
32 |
33 | attrs.xml
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 | colors.xml 定义了一些UI标注的颜色值
68 |
69 | ```
70 |
71 |
72 | #3F51B5
73 | #303F9F
74 | #FF4081
75 |
76 |
77 | #ff4d4d
78 | #00d68d
79 | #bfbfbf
80 |
81 | ```
82 | dimens.xml 定义了一些UI标注的尺寸值
83 |
84 | ```
85 |
86 |
87 |
88 | 48dp
89 |
90 | 31dp
91 |
92 |
93 | 2.5dp
94 |
95 |
96 | 28dp
97 |
98 | 42dp
99 |
100 | 3dp
101 |
102 |
103 | 14dp
104 |
105 | 3dp
106 |
107 | 2dp
108 |
109 |
110 | 2dp
111 |
112 |
113 | 22dp
114 |
115 | 35dp
116 |
117 |
118 | ```
119 |
120 | ## 3.2 实现自定义View
121 |
122 | 创建PowerConsumptionRankingsBatteryView继承View,
123 | 在View的构造方法中,获取我们需要的自定义样式,重写onMesure,onDraw方法。
124 |
125 | ```
126 | package com.oyp.battery;
127 |
128 | import android.content.Context;
129 | import android.content.res.TypedArray;
130 | import android.graphics.Canvas;
131 | import android.graphics.DrawFilter;
132 | import android.graphics.Paint;
133 | import android.graphics.PaintFlagsDrawFilter;
134 | import android.graphics.RectF;
135 | import android.support.annotation.Nullable;
136 | import android.util.AttributeSet;
137 | import android.view.View;
138 |
139 |
140 | /**
141 | * 自定义 耗电排行内的 电池图标
142 | *
143 | * created by OuyangPeng at 2018/6/10 下午 05:57
144 | *
145 | * @author OuyangPeng
146 | */
147 | public class PowerConsumptionRankingsBatteryView extends View {
148 | /**
149 | * 电池最大电量
150 | */
151 | public static final int MAX_LEVEL = 100;
152 | /**
153 | * 电池默认电量
154 | */
155 | public static final int DEFAULT_LEVEL = 40;
156 |
157 | /**
158 | * 自定义View的宽
159 | */
160 | private int width;
161 | /**
162 | * 自定义View的高
163 | */
164 | private int height;
165 | /**
166 | * 抗锯齿标志
167 | */
168 | private DrawFilter drawFilter;
169 | /**
170 | * 电池外壳 厚度
171 | */
172 | private int shellStrokeWidth;
173 | /**
174 | * 电池外壳 圆角
175 | */
176 | private int shellCornerRadius;
177 | /**
178 | * 电池外壳 宽度
179 | */
180 | private int shellWidth;
181 | /**
182 | * 电池外壳 宽度
183 | */
184 | private int shellHeight;
185 | /**
186 | * 电池头 圆角
187 | */
188 | private int shellHeadCornerRadius;
189 | /**
190 | * 电池头 宽度
191 | */
192 | private int shellHeadWidth;
193 | /**
194 | * 电池头 高度
195 | */
196 | private int shellHeadHeight;
197 |
198 | /**
199 | * 电池宽度
200 | */
201 | private int levelWidth;
202 | /**
203 | * 电池最大高度
204 | */
205 | private int levelMaxHeight;
206 | /**
207 | * 电池高度
208 | */
209 | private int levelHeight = 100;
210 |
211 | /**
212 | * 电池外壳和电池等级直接的间距
213 | */
214 | private int gap;
215 |
216 | /**
217 | * 电池外壳 画笔
218 | */
219 | private Paint shellPaint;
220 | /**
221 | * 电池外壳
222 | */
223 | private RectF shellRectF;
224 | /**
225 | * 电池头
226 | */
227 | private RectF shellHeadRect;
228 |
229 | /**
230 | * 电池电量 画笔
231 | */
232 | private Paint levelPaint;
233 | /**
234 | * 电池电量
235 | */
236 | private RectF levelRect;
237 |
238 | /**
239 | * 低电颜色
240 | */
241 | private int lowerPowerColor;
242 | /**
243 | * 在线颜色
244 | */
245 | private int onlineColor;
246 | /**
247 | * 离线颜色
248 | */
249 | private int offlineColor;
250 |
251 | public PowerConsumptionRankingsBatteryView(Context context) {
252 | super(context);
253 | }
254 |
255 | public PowerConsumptionRankingsBatteryView(Context context, @Nullable AttributeSet attrs) {
256 | super(context, attrs);
257 | drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
258 |
259 | initTypeArray(context, attrs);
260 |
261 | //设置 电池壳画笔的相关属性
262 | shellPaint = new Paint();
263 | shellPaint.setAntiAlias(true);
264 | shellPaint.setColor(onlineColor);
265 | shellPaint.setStrokeWidth(shellStrokeWidth);
266 | shellPaint.setAntiAlias(true);
267 |
268 | //设置 电池画笔的相关属性
269 | levelPaint = new Paint();
270 | levelPaint.setColor(onlineColor);
271 | levelPaint.setStyle(Paint.Style.FILL);
272 | levelPaint.setStrokeWidth(levelWidth);
273 |
274 | shellRectF = new RectF();
275 | shellHeadRect = new RectF();
276 | levelRect = new RectF();
277 | }
278 |
279 | /**
280 | * 初始化自定义属性
281 | */
282 | private void initTypeArray(Context context, @Nullable AttributeSet attrs) {
283 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PowerConsumptionRankingsBatteryView);
284 | lowerPowerColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryLowerPowerColor,
285 | getResources().getColor(R.color.lowerPowerColor));
286 | onlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOnlineColor,
287 | getResources().getColor(R.color.onlineColor));
288 | offlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOfflineColor,
289 | getResources().getColor(R.color.offlineColor));
290 | //外壳的相关信息
291 | shellCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellCornerRadius,
292 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_corner));
293 | shellWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellWidth,
294 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_width));
295 | shellHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeight,
296 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_height));
297 | shellStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellStrokeWidth,
298 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_stroke_width));
299 |
300 | //电池头的相关信息
301 | shellHeadCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadCornerRadius,
302 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_corner));
303 | shellHeadWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadWidth,
304 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_width));
305 | shellHeadHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadHeight,
306 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_height));
307 |
308 | //电池最大高度
309 | levelMaxHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelMaxHeight,
310 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_max_height));
311 | //电池宽度
312 | levelWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelWidth,
313 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_width));
314 |
315 | //电池外壳和电池等级直接的间距
316 | gap = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryGap,
317 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_gap));
318 | //回收typedArray
319 | typedArray.recycle();
320 | }
321 |
322 | @Override
323 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
324 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
325 | //对View上的內容进行测量后得到的View內容占据的宽度
326 | width = getMeasuredWidth();
327 | //对View上的內容进行测量后得到的View內容占据的高度
328 | height = getMeasuredHeight();
329 | }
330 |
331 | @Override
332 | protected void onDraw(Canvas canvas) {
333 | super.onDraw(canvas);
334 | canvas.setDrawFilter(drawFilter);
335 |
336 | // 电池头 矩形的坐标
337 |
338 | //坐标 left:控件整体宽度的一半 减去 电池头宽度的一半
339 | shellHeadRect.left = width / 2 - shellHeadWidth / 2;
340 | //坐标 top: 0
341 | shellHeadRect.top = 0;
342 | //坐标 right:控件整体宽度的一半 加上 电池头宽度的一半
343 | shellHeadRect.right = width / 2 + shellHeadWidth / 2;
344 | //坐标 bottom:电池头的高度
345 | shellHeadRect.bottom = shellHeadHeight;
346 |
347 | // 电池壳 矩形的坐标
348 |
349 | //坐标 left:电池壳厚度的一半
350 | shellRectF.left = shellStrokeWidth / 2;
351 | //坐标 left:电池壳厚度的一半 加上 电池头的高度
352 | shellRectF.top = shellStrokeWidth / 2 + shellHeadHeight;
353 | //坐标 right:控件整体宽度 减去 电池壳厚度的一半
354 | shellRectF.right = width - shellStrokeWidth / 2;
355 | //坐标 bottom:控件整体高度 减去 电池壳厚度的一半
356 | shellRectF.bottom = height - shellStrokeWidth / 2;
357 |
358 | // 电池电量 矩形的坐标
359 |
360 | //坐标 left:电池壳厚度的一半 加上 电池外壳和电池等级直接的间距
361 | levelRect.left = shellStrokeWidth + gap;
362 |
363 | //电池满格时候的最大高度 :(控件整体高度 减去电池壳厚度 减去电池头高度 减去 电池外壳和电池等级直接的间距的两倍)
364 | //topOffset: 电池满格时候的最大高度 * (电池满格100 - 当前的电量)/ 电池满格100
365 | float topOffset = (height - shellHeadHeight - gap * 2 - shellStrokeWidth) * (MAX_LEVEL - levelHeight) / MAX_LEVEL;
366 | //坐标 top:电池头的高度 + 电池壳的厚度 + 电池外壳和电池等级直接的间距 + topOffset
367 | levelRect.top = shellHeadHeight + shellStrokeWidth + gap + topOffset;
368 |
369 | //坐标 right:控件整体宽度 减去 电池壳厚度的一半 减去 电池外壳和电池等级直接的间距
370 | levelRect.right = width - shellStrokeWidth - gap;
371 |
372 | //坐标 bottom:控件整体宽度 减去 电池壳厚度的一半 减去 电池外壳和电池等级直接的间距
373 | levelRect.bottom = height - shellStrokeWidth - gap;
374 |
375 | //绘制电池头
376 | shellPaint.setStyle(Paint.Style.FILL);
377 | canvas.drawRoundRect(shellHeadRect, shellHeadCornerRadius, shellHeadCornerRadius, shellPaint);
378 | //由于电池头的左下角和右下角不是圆角的,因此我们需要画一个矩形 覆盖圆角
379 | //绘制左下角矩形
380 | canvas.drawRect(shellHeadRect.left, shellHeadRect.bottom - shellHeadCornerRadius,
381 | shellHeadRect.left + shellHeadCornerRadius, shellHeadRect.bottom, shellPaint);
382 | //绘制右下角矩形
383 | canvas.drawRect(shellHeadRect.right - shellHeadCornerRadius, shellHeadRect.bottom - shellHeadCornerRadius,
384 | shellHeadRect.right, shellHeadRect.bottom, shellPaint);
385 |
386 | //绘制电池壳
387 | shellPaint.setStyle(Paint.Style.STROKE);
388 | canvas.drawRoundRect(shellRectF, shellCornerRadius, shellCornerRadius, shellPaint);
389 |
390 | //绘制电池等级
391 | canvas.drawRect(levelRect, levelPaint);
392 | }
393 |
394 | /**
395 | * 设置电池电量
396 | *
397 | * @param level
398 | */
399 | public void setLevelHeight(int level) {
400 | this.levelHeight = level;
401 | if (this.levelHeight < 0) {
402 | levelHeight = MAX_LEVEL;
403 | } else if (this.levelHeight > MAX_LEVEL) {
404 | levelHeight = MAX_LEVEL;
405 | }
406 | postInvalidate();
407 | }
408 |
409 | /**
410 | * 设置在线 重绘
411 | */
412 | public void setOnline() {
413 | shellPaint.setColor(onlineColor);
414 | levelPaint.setColor(onlineColor);
415 | postInvalidate();
416 | }
417 |
418 | /**
419 | * 设置离线 重绘
420 | */
421 | public void setOffline() {
422 | shellPaint.setColor(offlineColor);
423 | levelPaint.setColor(offlineColor);
424 | postInvalidate();
425 | }
426 |
427 | /**
428 | * 设置低电 重绘
429 | */
430 | public void setLowerPower() {
431 | shellPaint.setColor(lowerPowerColor);
432 | levelPaint.setColor(lowerPowerColor);
433 | postInvalidate();
434 | }
435 | }
436 |
437 | ```
438 |
439 | ## 3.3 使用我们的自定义View
440 |
441 | activity_main.xml 中使用我们的自定义View
442 |
443 | ```
444 |
445 |
452 |
453 |
462 |
463 |
474 |
475 |
476 | ```
477 |
478 | ## 3.4 动态展示不同状态下的电池View
479 |
480 | MainActivity.java 文件中 动态更新电池的状态
481 |
482 | ```
483 | package com.oyp.battery;
484 |
485 | import android.os.Handler;
486 | import android.os.Message;
487 | import android.support.v7.app.AppCompatActivity;
488 | import android.os.Bundle;
489 |
490 | import java.lang.ref.WeakReference;
491 | import java.util.Timer;
492 | import java.util.TimerTask;
493 | /**
494 | * 自定义View 展示界面
495 | *
496 | * created by OuyangPeng at 2018/6/15 下午 03:33
497 | * @author OuyangPeng
498 | */
499 | public class MainActivity extends AppCompatActivity implements BaseHandlerCallBack {
500 |
501 | private PowerConsumptionRankingsBatteryView mPowerConsumptionRankingsBatteryView;
502 | private int power;
503 |
504 | private NoLeakHandler mHandler;
505 |
506 | @Override
507 | protected void onCreate(Bundle savedInstanceState) {
508 | super.onCreate(savedInstanceState);
509 | setContentView(R.layout.activity_main);
510 | mHandler = new NoLeakHandler(this);
511 | mPowerConsumptionRankingsBatteryView = (PowerConsumptionRankingsBatteryView) findViewById(R.id.mPowerConsumptionRankingsBatteryView);
512 |
513 | new Timer().schedule(new TimerTask() {
514 | @Override
515 | public void run() {
516 | mHandler.sendEmptyMessage(0);
517 | }
518 | }, 0, 100);
519 | }
520 |
521 | @Override
522 | protected void onDestroy() {
523 | super.onDestroy();
524 | mHandler.removeCallbacksAndMessages(null);
525 | }
526 |
527 | @Override
528 | public void callBack(Message msg) {
529 | switch (msg.what) {
530 | case 0:
531 | mPowerConsumptionRankingsBatteryView.setLevelHeight(power += 5);
532 | if (power == 100) {
533 | power = 0;
534 | }
535 | if (power < 30) {
536 | mPowerConsumptionRankingsBatteryView.setLowerPower();
537 | } else if (power < 60) {
538 | mPowerConsumptionRankingsBatteryView.setOffline();
539 | } else {
540 | mPowerConsumptionRankingsBatteryView.setOnline();
541 | }
542 | break;
543 | default:
544 | break;
545 | }
546 | }
547 |
548 | private static class NoLeakHandler extends Handler {
549 | private WeakReference wr;
550 |
551 | public NoLeakHandler(T t) {
552 | wr = new WeakReference<>(t);
553 | }
554 |
555 | @Override
556 | public void handleMessage(Message msg) {
557 | super.handleMessage(msg);
558 | T t = wr.get();
559 | if (t != null) {
560 | t.callBack(msg);
561 | }
562 | }
563 | }
564 | }
565 |
566 |
567 | ```
568 | BaseHandlerCallBack 定义的接口
569 | ```
570 | package com.oyp.battery;
571 |
572 | import android.os.Message;
573 |
574 | public interface BaseHandlerCallBack {
575 | public void callBack(Message msg);
576 | }
577 | ```
578 |
579 | ## 3.5 效果
580 | 
581 |
582 |
583 | # 四、github源代码
584 |
585 | https://github.com/ouyangpeng/PowerConsumptionRankingsBatteryView
586 |
587 | ------
588 |
589 | >作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
590 | 转载请保留原文地址:https://blog.csdn.net/ouyang_peng/article/details/80372805
591 | >
592 | >
593 |
594 | ####如果本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行打赏。
595 | 
596 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.oyp.battery"
7 | minSdkVersion 15
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:28.+'
24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/oyp/battery/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.oyp.battery;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.oyp.battery", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/oyp/battery/BaseHandlerCallBack.java:
--------------------------------------------------------------------------------
1 | package com.oyp.battery;
2 |
3 | import android.os.Message;
4 |
5 | public interface BaseHandlerCallBack {
6 | public void callBack(Message msg);
7 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/oyp/battery/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.oyp.battery;
2 |
3 | import android.os.Handler;
4 | import android.os.Message;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.os.Bundle;
7 |
8 | import java.lang.ref.WeakReference;
9 | import java.util.Timer;
10 | import java.util.TimerTask;
11 | /**
12 | * 自定义View 展示界面
13 | *
14 | * created by OuyangPeng at 2018/6/15 下午 03:33
15 | * @author OuyangPeng
16 | */
17 | public class MainActivity extends AppCompatActivity implements BaseHandlerCallBack {
18 |
19 | private PowerConsumptionRankingsBatteryView mPowerConsumptionRankingsBatteryView;
20 | private int power;
21 |
22 | private NoLeakHandler mHandler;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_main);
28 | mHandler = new NoLeakHandler(this);
29 | mPowerConsumptionRankingsBatteryView = (PowerConsumptionRankingsBatteryView) findViewById(R.id.mPowerConsumptionRankingsBatteryView);
30 |
31 | new Timer().schedule(new TimerTask() {
32 | @Override
33 | public void run() {
34 | mHandler.sendEmptyMessage(0);
35 | }
36 | }, 0, 100);
37 | }
38 |
39 | @Override
40 | protected void onDestroy() {
41 | super.onDestroy();
42 | mHandler.removeCallbacksAndMessages(null);
43 | }
44 |
45 | @Override
46 | public void callBack(Message msg) {
47 | switch (msg.what) {
48 | case 0:
49 | mPowerConsumptionRankingsBatteryView.setLevelHeight(power += 5);
50 | if (power == 100) {
51 | power = 0;
52 | }
53 | if (power < 30) {
54 | mPowerConsumptionRankingsBatteryView.setLowerPower();
55 | } else if (power < 60) {
56 | mPowerConsumptionRankingsBatteryView.setOffline();
57 | } else {
58 | mPowerConsumptionRankingsBatteryView.setOnline();
59 | }
60 | break;
61 | default:
62 | break;
63 | }
64 | }
65 |
66 | private static class NoLeakHandler extends Handler {
67 | private WeakReference wr;
68 |
69 | public NoLeakHandler(T t) {
70 | wr = new WeakReference<>(t);
71 | }
72 |
73 | @Override
74 | public void handleMessage(Message msg) {
75 | super.handleMessage(msg);
76 | T t = wr.get();
77 | if (t != null) {
78 | t.callBack(msg);
79 | }
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/app/src/main/java/com/oyp/battery/PowerConsumptionRankingsBatteryView.java:
--------------------------------------------------------------------------------
1 | package com.oyp.battery;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Canvas;
6 | import android.graphics.DrawFilter;
7 | import android.graphics.Paint;
8 | import android.graphics.PaintFlagsDrawFilter;
9 | import android.graphics.RectF;
10 | import android.support.annotation.Nullable;
11 | import android.util.AttributeSet;
12 | import android.view.View;
13 |
14 |
15 | /**
16 | * 自定义 耗电排行内的 电池图标
17 | *
18 | * created by OuyangPeng at 2018/6/10 下午 05:57
19 | *
20 | * @author OuyangPeng
21 | */
22 | public class PowerConsumptionRankingsBatteryView extends View {
23 | /**
24 | * 电池最大电量
25 | */
26 | public static final int MAX_LEVEL = 100;
27 | /**
28 | * 电池默认电量
29 | */
30 | public static final int DEFAULT_LEVEL = 40;
31 |
32 | /**
33 | * 自定义View的宽
34 | */
35 | private int width;
36 | /**
37 | * 自定义View的高
38 | */
39 | private int height;
40 | /**
41 | * 抗锯齿标志
42 | */
43 | private DrawFilter drawFilter;
44 | /**
45 | * 电池外壳 厚度
46 | */
47 | private int shellStrokeWidth;
48 | /**
49 | * 电池外壳 圆角
50 | */
51 | private int shellCornerRadius;
52 | /**
53 | * 电池外壳 宽度
54 | */
55 | private int shellWidth;
56 | /**
57 | * 电池外壳 宽度
58 | */
59 | private int shellHeight;
60 | /**
61 | * 电池头 圆角
62 | */
63 | private int shellHeadCornerRadius;
64 | /**
65 | * 电池头 宽度
66 | */
67 | private int shellHeadWidth;
68 | /**
69 | * 电池头 高度
70 | */
71 | private int shellHeadHeight;
72 |
73 | /**
74 | * 电池宽度
75 | */
76 | private int levelWidth;
77 | /**
78 | * 电池最大高度
79 | */
80 | private int levelMaxHeight;
81 | /**
82 | * 电池高度
83 | */
84 | private int levelHeight = 100;
85 |
86 | /**
87 | * 电池外壳和电池等级直接的间距
88 | */
89 | private int gap;
90 |
91 | /**
92 | * 电池外壳 画笔
93 | */
94 | private Paint shellPaint;
95 | /**
96 | * 电池外壳
97 | */
98 | private RectF shellRectF;
99 | /**
100 | * 电池头
101 | */
102 | private RectF shellHeadRect;
103 |
104 | /**
105 | * 电池电量 画笔
106 | */
107 | private Paint levelPaint;
108 | /**
109 | * 电池电量
110 | */
111 | private RectF levelRect;
112 |
113 | /**
114 | * 低电颜色
115 | */
116 | private int lowerPowerColor;
117 | /**
118 | * 在线颜色
119 | */
120 | private int onlineColor;
121 | /**
122 | * 离线颜色
123 | */
124 | private int offlineColor;
125 |
126 | public PowerConsumptionRankingsBatteryView(Context context) {
127 | super(context);
128 | }
129 |
130 | public PowerConsumptionRankingsBatteryView(Context context, @Nullable AttributeSet attrs) {
131 | super(context, attrs);
132 | drawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
133 |
134 | initTypeArray(context, attrs);
135 |
136 | //设置 电池壳画笔的相关属性
137 | shellPaint = new Paint();
138 | shellPaint.setAntiAlias(true);
139 | shellPaint.setColor(onlineColor);
140 | shellPaint.setStrokeWidth(shellStrokeWidth);
141 | shellPaint.setAntiAlias(true);
142 |
143 | //设置 电池画笔的相关属性
144 | levelPaint = new Paint();
145 | levelPaint.setColor(onlineColor);
146 | levelPaint.setStyle(Paint.Style.FILL);
147 | levelPaint.setStrokeWidth(levelWidth);
148 |
149 | shellRectF = new RectF();
150 | shellHeadRect = new RectF();
151 | levelRect = new RectF();
152 | }
153 |
154 | /**
155 | * 初始化自定义属性
156 | */
157 | private void initTypeArray(Context context, @Nullable AttributeSet attrs) {
158 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PowerConsumptionRankingsBatteryView);
159 | lowerPowerColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryLowerPowerColor,
160 | getResources().getColor(R.color.lowerPowerColor));
161 | onlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOnlineColor,
162 | getResources().getColor(R.color.onlineColor));
163 | offlineColor = typedArray.getColor(R.styleable.PowerConsumptionRankingsBatteryView_batteryOfflineColor,
164 | getResources().getColor(R.color.offlineColor));
165 | //外壳的相关信息
166 | shellCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellCornerRadius,
167 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_corner));
168 | shellWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellWidth,
169 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_width));
170 | shellHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeight,
171 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_height));
172 | shellStrokeWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellStrokeWidth,
173 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_shell_stroke_width));
174 |
175 | //电池头的相关信息
176 | shellHeadCornerRadius = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadCornerRadius,
177 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_corner));
178 | shellHeadWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadWidth,
179 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_width));
180 | shellHeadHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryShellHeadHeight,
181 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_head_height));
182 |
183 | //电池最大高度
184 | levelMaxHeight = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelMaxHeight,
185 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_max_height));
186 | //电池宽度
187 | levelWidth = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryLevelWidth,
188 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_level_width));
189 |
190 | //电池外壳和电池等级直接的间距
191 | gap = typedArray.getDimensionPixelOffset(R.styleable.PowerConsumptionRankingsBatteryView_batteryGap,
192 | getResources().getDimensionPixelOffset(R.dimen.power_consumption_rankings_dimen_main_battery_view_gap));
193 | //回收typedArray
194 | typedArray.recycle();
195 | }
196 |
197 | @Override
198 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
199 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
200 | //对View上的內容进行测量后得到的View內容占据的宽度
201 | width = getMeasuredWidth();
202 | //对View上的內容进行测量后得到的View內容占据的高度
203 | height = getMeasuredHeight();
204 | }
205 |
206 | @Override
207 | protected void onDraw(Canvas canvas) {
208 | super.onDraw(canvas);
209 | canvas.setDrawFilter(drawFilter);
210 |
211 | // 电池头 矩形的坐标
212 |
213 | //坐标 left:控件整体宽度的一半 减去 电池头宽度的一半
214 | shellHeadRect.left = width / 2 - shellHeadWidth / 2;
215 | //坐标 top: 0
216 | shellHeadRect.top = 0;
217 | //坐标 right:控件整体宽度的一半 加上 电池头宽度的一半
218 | shellHeadRect.right = width / 2 + shellHeadWidth / 2;
219 | //坐标 bottom:电池头的高度
220 | shellHeadRect.bottom = shellHeadHeight;
221 |
222 | // 电池壳 矩形的坐标
223 |
224 | //坐标 left:电池壳厚度的一半
225 | shellRectF.left = shellStrokeWidth / 2;
226 | //坐标 left:电池壳厚度的一半 加上 电池头的高度
227 | shellRectF.top = shellStrokeWidth / 2 + shellHeadHeight;
228 | //坐标 right:控件整体宽度 减去 电池壳厚度的一半
229 | shellRectF.right = width - shellStrokeWidth / 2;
230 | //坐标 bottom:控件整体高度 减去 电池壳厚度的一半
231 | shellRectF.bottom = height - shellStrokeWidth / 2;
232 |
233 | // 电池电量 矩形的坐标
234 |
235 | //坐标 left:电池壳厚度的一半 加上 电池外壳和电池等级直接的间距
236 | levelRect.left = shellStrokeWidth + gap;
237 |
238 | //电池满格时候的最大高度 :(控件整体高度 减去电池壳厚度 减去电池头高度 减去 电池外壳和电池等级直接的间距的两倍)
239 | //topOffset: 电池满格时候的最大高度 * (电池满格100 - 当前的电量)/ 电池满格100
240 | float topOffset = (height - shellHeadHeight - gap * 2 - shellStrokeWidth) * (MAX_LEVEL - levelHeight) / MAX_LEVEL;
241 | //坐标 top:电池头的高度 + 电池壳的厚度 + 电池外壳和电池等级直接的间距 + topOffset
242 | levelRect.top = shellHeadHeight + shellStrokeWidth + gap + topOffset;
243 |
244 | //坐标 right:控件整体宽度 减去 电池壳厚度的一半 减去 电池外壳和电池等级直接的间距
245 | levelRect.right = width - shellStrokeWidth - gap;
246 |
247 | //坐标 bottom:控件整体宽度 减去 电池壳厚度的一半 减去 电池外壳和电池等级直接的间距
248 | levelRect.bottom = height - shellStrokeWidth - gap;
249 |
250 | //绘制电池头
251 | shellPaint.setStyle(Paint.Style.FILL);
252 | canvas.drawRoundRect(shellHeadRect, shellHeadCornerRadius, shellHeadCornerRadius, shellPaint);
253 | //由于电池头的左下角和右下角不是圆角的,因此我们需要画一个矩形 覆盖圆角
254 | //绘制左下角矩形
255 | canvas.drawRect(shellHeadRect.left, shellHeadRect.bottom - shellHeadCornerRadius,
256 | shellHeadRect.left + shellHeadCornerRadius, shellHeadRect.bottom, shellPaint);
257 | //绘制右下角矩形
258 | canvas.drawRect(shellHeadRect.right - shellHeadCornerRadius, shellHeadRect.bottom - shellHeadCornerRadius,
259 | shellHeadRect.right, shellHeadRect.bottom, shellPaint);
260 |
261 | //绘制电池壳
262 | shellPaint.setStyle(Paint.Style.STROKE);
263 | canvas.drawRoundRect(shellRectF, shellCornerRadius, shellCornerRadius, shellPaint);
264 |
265 | //绘制电池等级
266 | canvas.drawRect(levelRect, levelPaint);
267 | }
268 |
269 | /**
270 | * 设置电池电量
271 | *
272 | * @param level
273 | */
274 | public void setLevelHeight(int level) {
275 | this.levelHeight = level;
276 | if (this.levelHeight < 0) {
277 | levelHeight = MAX_LEVEL;
278 | } else if (this.levelHeight > MAX_LEVEL) {
279 | levelHeight = MAX_LEVEL;
280 | }
281 | postInvalidate();
282 | }
283 |
284 | /**
285 | * 设置在线 重绘
286 | */
287 | public void setOnline() {
288 | shellPaint.setColor(onlineColor);
289 | levelPaint.setColor(onlineColor);
290 | postInvalidate();
291 | }
292 |
293 | /**
294 | * 设置离线 重绘
295 | */
296 | public void setOffline() {
297 | shellPaint.setColor(offlineColor);
298 | levelPaint.setColor(offlineColor);
299 | postInvalidate();
300 | }
301 |
302 | /**
303 | * 设置低电 重绘
304 | */
305 | public void setLowerPower() {
306 | shellPaint.setColor(lowerPowerColor);
307 | levelPaint.setColor(lowerPowerColor);
308 | postInvalidate();
309 | }
310 | }
311 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
8 | #ff4d4d
9 | #00d68d
10 | #bfbfbf
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 48dp
5 |
6 | 31dp
7 |
8 |
9 | 2.5dp
10 |
11 |
12 | 28dp
13 |
14 | 42dp
15 |
16 | 3dp
17 |
18 |
19 | 14dp
20 |
21 | 3dp
22 |
23 | 2dp
24 |
25 |
26 | 2dp
27 |
28 |
29 | 22dp
30 |
31 | 35dp
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | 自定义电池View 【作者:欧阳鹏】
3 | 欧阳鹏 https://blog.csdn.net/ouyang_peng/
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/oyp/battery/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.oyp.battery;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.1.3'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Jun 15 15:00:11 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/screenshot/v1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/screenshot/v1.png
--------------------------------------------------------------------------------
/screenshot/v2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/screenshot/v2.png
--------------------------------------------------------------------------------
/screenshot/v3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/screenshot/v3.png
--------------------------------------------------------------------------------
/screenshot/v4.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ouyangpeng/PowerConsumptionRankingsBatteryView/5f233a6e0de93daeb866bd3638ff1108546e49ad/screenshot/v4.mp4
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------