├── .gitignore ├── .idea ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── sjx │ │ └── batteryview │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── sjx │ │ │ └── batteryview │ │ │ └── MainActivity.kt │ └── 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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── sjx │ └── batteryview │ └── ExampleUnitTest.kt ├── batteryviewlibrary ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── sjx │ │ └── batteryviewlibrary │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── sjx │ │ │ └── batteryviewlibrary │ │ │ ├── BatteryView.java │ │ │ ├── BatteryViewOrientation.java │ │ │ └── OnBatteryPowerListener.java │ └── res │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── sjx │ └── batteryviewlibrary │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── imgs └── battery.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 10 | 11 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | xmlns:android 20 | 21 | ^$ 22 | 23 | 24 | 25 |
26 |
27 | 28 | 29 | 30 | xmlns:.* 31 | 32 | ^$ 33 | 34 | 35 | BY_NAME 36 | 37 |
38 |
39 | 40 | 41 | 42 | .*:id 43 | 44 | http://schemas.android.com/apk/res/android 45 | 46 | 47 | 48 |
49 |
50 | 51 | 52 | 53 | .*:name 54 | 55 | http://schemas.android.com/apk/res/android 56 | 57 | 58 | 59 |
60 |
61 | 62 | 63 | 64 | name 65 | 66 | ^$ 67 | 68 | 69 | 70 |
71 |
72 | 73 | 74 | 75 | style 76 | 77 | ^$ 78 | 79 | 80 | 81 |
82 |
83 | 84 | 85 | 86 | .* 87 | 88 | ^$ 89 | 90 | 91 | BY_NAME 92 | 93 |
94 |
95 | 96 | 97 | 98 | .* 99 | 100 | http://schemas.android.com/apk/res/android 101 | 102 | 103 | ANDROID_ATTRIBUTE_ORDER 104 | 105 |
106 |
107 | 108 | 109 | 110 | .* 111 | 112 | .* 113 | 114 | 115 | BY_NAME 116 | 117 |
118 |
119 |
120 |
121 | 122 | 124 |
125 |
-------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Android自定义电池 3 | > 跟随系统电量自动更新,高度简化使用,一行代码即可实现电量图标的自动变化,无需手动操作进行管理 4 | 5 | [ ![Download](https://api.bintray.com/packages/swallowsonny/ext/BatteryView/images/download.svg?version=1.0.0) ](https://bintray.com/swallowsonny/ext/BatteryView/1.0.0/link) 6 | 7 | #### 成果图片 8 | ![Image](https://github.com/swallowsonny/BatteryView/blob/master/imgs/battery.png) 9 | 10 | 11 | 12 | #### 项目引入 13 | - 项目根目录的build.gradle中添加仓库 14 | ``` 15 | repositories { 16 | maven { 17 | url "https://dl.bintray.com/swallowsonny/ext" 18 | } 19 | } 20 | ``` 21 | - 在app目录下的build.gradle中添加 22 | ``` 23 | implementation 'com.sjx.batteryview:BatteryView:1.0.0' 24 | ``` 25 | 26 | #### 基本使用 27 | - xml中引入 28 | ```xml 29 | 42 | ``` 43 | 44 | - Activity的onCreate()方法中初始化 45 | ```kotlin 46 | // 初始化 47 | batteryView.setLifecycleOwner(this) 48 | // 电量回调监听,当前电量值,最大值100 49 | batteryView.setOnBatteryPowerListener { } 50 | ``` 51 | 52 | #### 常用属性 53 | - `bv_orientation` 54 | 55 | 电池头方向:`horizontal_left` | `horizontal_right` | `vertical_top` | `vertical_bottom` 56 | - `bv_border_padding`: 电池外边框与内部实心电量部分的间距 57 | - `bv_border_width`: 电池外边框线条宽度 58 | - `bv_radis`: 电池矩形圆角大小 59 | - `bv_border_color`: 电池边框颜色 60 | - `bv_power_color_low`: 低电量显示颜色,默认红色 61 | - `bv_power_value_low`: 低电量阈值,默认为10,满电量100 62 | - `bv_power_color_medium`: 中电量颜色,默认黄色 63 | - `bv_power_value_medium`: 中电量阈值,默认20 64 | - `bv_power_color_high`: 充电中,高电量的显示颜色 65 | - `bv_no_charging_color_high`: 未充电下高电量的显示颜色 66 | - `bv_header_width`: 电池头的宽度 67 | - `bv_header_color`: 电池头的颜色 68 | - `bv_charging_speed`: 电池充电时,显示动画的速度,范围1-9,数值越大,速度越快 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 29 9 | defaultConfig { 10 | applicationId "com.sjx.batteryview" 11 | minSdkVersion 21 12 | targetSdkVersion 29 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 28 | implementation 'androidx.appcompat:appcompat:1.0.2' 29 | implementation 'androidx.core:core-ktx:1.0.2' 30 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 31 | testImplementation 'junit:junit:4.12' 32 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 33 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 34 | implementation project(path: ':batteryviewlibrary') 35 | } 36 | -------------------------------------------------------------------------------- /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/sjx/batteryview/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryview 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.sjx.batteryview", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/sjx/batteryview/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryview 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import androidx.appcompat.app.AppCompatActivity 6 | import kotlinx.android.synthetic.main.activity_main.* 7 | 8 | 9 | class MainActivity : AppCompatActivity() { 10 | override fun onCreate(savedInstanceState: Bundle?) { 11 | super.onCreate(savedInstanceState) 12 | setContentView(R.layout.activity_main) 13 | bv_1.setLifecycleOwner(this) 14 | bv_2.setLifecycleOwner(this) 15 | bv_3.setLifecycleOwner(this) 16 | bv_4.setLifecycleOwner(this) 17 | bv_1.setOnBatteryPowerListener { tv_power.text = "$it%" } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /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 | 38 | 39 | 56 | 57 | 72 | 73 | 88 | -------------------------------------------------------------------------------- /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/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | #000000 7 | #FFFFFF 8 | 9 | #FF0000 10 | #00FF00 11 | #FFFFFF 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | BatteryView 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/sjx/batteryview/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryview 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /batteryviewlibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /batteryviewlibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | android { 5 | compileSdkVersion 29 6 | 7 | 8 | defaultConfig { 9 | minSdkVersion 21 10 | targetSdkVersion 29 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles 'consumer-rules.pro' 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | } 26 | 27 | dependencies { 28 | implementation fileTree(dir: 'libs', include: ['*.jar']) 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 30 | implementation 'androidx.appcompat:appcompat:1.0.2' 31 | implementation 'androidx.core:core-ktx:1.0.2' 32 | testImplementation 'junit:junit:4.12' 33 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 35 | } 36 | 37 | //添加下面这句 38 | apply plugin: 'com.novoda.bintray-release' 39 | 40 | publish { 41 | repoName="ext"//仓库名 你的仓库名称,没有填写默认仓库是maven//这也是很多人上传仓库不对名问题最多情况, 42 | userOrg = 'swallowsonny' //bintray注册的用户名 43 | groupId = 'com.sjx.batteryview'// //compile引用时的第1部分 44 | artifactId = 'BatteryView'////发布到JCenter上的项目名字,compile引用时的第2部分项目名 45 | publishVersion = '1.0.0'//版本号 ,compile引用时的最后部分,下次更新是只需要更改版本号即可 46 | desc = 'A custom view that displays the power in real time'//描述,不重要 47 | website = 'https://github.com/swallowsonny/BatteryView' //项目的GitHub网站 48 | } 49 | allprojects { 50 | repositories { 51 | jcenter() 52 | } 53 | //加上这些 54 | tasks.withType(Javadoc) { 55 | options{ encoding "UTF-8" 56 | charSet 'UTF-8' 57 | links "http://docs.oracle.com/javase/7/docs/api" 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /batteryviewlibrary/consumer-rules.pro: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/batteryviewlibrary/consumer-rules.pro -------------------------------------------------------------------------------- /batteryviewlibrary/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 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/androidTest/java/com/sjx/batteryviewlibrary/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryviewlibrary 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.sjx.batteryviewlibrary.test", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/java/com/sjx/batteryviewlibrary/BatteryView.java: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryviewlibrary; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.IntentFilter; 7 | import android.content.res.TypedArray; 8 | import android.graphics.Canvas; 9 | import android.graphics.Color; 10 | import android.graphics.Paint; 11 | import android.graphics.RectF; 12 | import android.os.BatteryManager; 13 | import android.os.Build; 14 | import android.os.Handler; 15 | import android.util.AttributeSet; 16 | import android.util.Log; 17 | import android.view.View; 18 | 19 | import androidx.annotation.NonNull; 20 | import androidx.annotation.Nullable; 21 | import androidx.lifecycle.Lifecycle; 22 | import androidx.lifecycle.LifecycleObserver; 23 | import androidx.lifecycle.LifecycleOwner; 24 | import androidx.lifecycle.OnLifecycleEvent; 25 | 26 | public class BatteryView extends View implements LifecycleObserver { 27 | // 电池方向 28 | private BatteryViewOrientation orientation; 29 | // 电池边框与内部电量的间隔 30 | private float borderPadding; 31 | // 电池边框宽度 32 | private float borderWidth; 33 | // 边框颜色 34 | private int borderColor; 35 | // 边框圆角 36 | private float radis; 37 | 38 | private float headerWidth; 39 | 40 | // 电池电量及对应的颜色 41 | // 低电量 默认值 0%-10% 红色 42 | private int lowColor; 43 | private int lowValue; 44 | // 中等电量 默认值 11%-20% 黄色 45 | private int mediumColor; 46 | private int mediumValue; 47 | // 高电量 默认值 21%-100% 白色 48 | private int highColor; 49 | private int headerColor; 50 | private int currentPower = 60; // 当前电量 51 | 52 | // 未充电时高电量颜色 53 | private int noChargingHighColor; 54 | 55 | private int chargingSpeed; 56 | 57 | private int width; 58 | private int height; 59 | private Context mContext; 60 | 61 | private Handler mHandler = new Handler(); 62 | private Runnable runnable; 63 | 64 | public BatteryView(Context context) { 65 | this(context, null); 66 | } 67 | 68 | public BatteryView(Context context, @Nullable AttributeSet attrs) { 69 | this(context, attrs, 0); 70 | } 71 | 72 | public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 73 | super(context, attrs, defStyleAttr); 74 | mContext = context; 75 | getAttrs(context, attrs); 76 | initPaints(); 77 | } 78 | 79 | private void initBattery() { 80 | try { 81 | BatteryManager manager = (BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE); 82 | if(Build.VERSION.SDK_INT >= 21 && manager != null){ 83 | // int property_charge_counter = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER); 84 | // int property_current_average = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE); 85 | // int property_current_now = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW); 86 | currentPower = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);///当前电量百分比 87 | // setPower(currentPower); 88 | // Log.i("BatteryManager1", property_charge_counter + ""); 89 | // Log.i("BatteryManager2", property_current_average + ""); 90 | // Log.i("BatteryManager3", property_current_now + ""); 91 | // Log.i("BatteryManager4", currentPower + ""); 92 | } 93 | } catch (Exception e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | 98 | private void getAttrs(Context context, AttributeSet attrs) { 99 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BatteryView); 100 | // 默认水平头朝右 101 | int orientationInt = ta.getInt(R.styleable.BatteryView_bv_orientation, 1); 102 | switch (orientationInt) { 103 | case 0: 104 | orientation = BatteryViewOrientation.HORIZONTAL_LEFT; 105 | break; 106 | case 1: 107 | orientation = BatteryViewOrientation.HORIZONTAL_RIGHT; 108 | break; 109 | case 2: 110 | orientation = BatteryViewOrientation.VERTICAL_TOP; 111 | break; 112 | case 3: 113 | orientation = BatteryViewOrientation.VERTICAL_BOTTOM; 114 | break; 115 | } 116 | // 电池内边距 117 | borderPadding = ta.getDimensionPixelSize(R.styleable.BatteryView_bv_border_padding, 2); 118 | // 电池边框厚度 119 | borderWidth = ta.getDimensionPixelSize(R.styleable.BatteryView_bv_border_width, 2); 120 | headerWidth = ta.getDimensionPixelSize(R.styleable.BatteryView_bv_header_width, 10); 121 | // 电池边框圆角 122 | radis = ta.getDimensionPixelSize(R.styleable.BatteryView_bv_radis, 2); 123 | // 电池边框颜色 124 | borderColor = ta.getColor(R.styleable.BatteryView_bv_border_color, Color.WHITE); 125 | 126 | // 电池实心部分 127 | lowColor = ta.getColor(R.styleable.BatteryView_bv_power_color_low, mContext.getResources().getColor(R.color.low)); 128 | lowValue = ta.getInt(R.styleable.BatteryView_bv_power_value_low, 10); 129 | mediumColor = ta.getColor(R.styleable.BatteryView_bv_power_color_medium, mContext.getResources().getColor(R.color.medium)); 130 | mediumValue = ta.getInt(R.styleable.BatteryView_bv_power_value_medium, 20); 131 | highColor = ta.getColor(R.styleable.BatteryView_bv_power_color_high, mContext.getResources().getColor(R.color.high)); 132 | 133 | headerColor = ta.getColor(R.styleable.BatteryView_bv_header_color, Color.WHITE); 134 | 135 | noChargingHighColor = ta.getColor(R.styleable.BatteryView_bv_no_charging_color_high, mContext.getResources().getColor(R.color.high)); 136 | 137 | chargingSpeed = ta.getInt(R.styleable.BatteryView_bv_charging_speed, 2) % 10; 138 | if(chargingSpeed == 0) chargingSpeed = 1; 139 | ta.recycle(); 140 | } 141 | 142 | private Paint borderPaint; 143 | private Paint powerPaint; 144 | private Paint headerPaint; 145 | 146 | // 初始化画笔 147 | private void initPaints() { 148 | // 149 | borderPaint = new Paint(); 150 | borderPaint.setAntiAlias(true); 151 | borderPaint.setStyle(Paint.Style.STROKE); 152 | borderPaint.setColor(borderColor); 153 | borderPaint.setStrokeWidth(borderWidth); 154 | // 电量的实心部分 155 | powerPaint = new Paint(); 156 | powerPaint.setAntiAlias(true); 157 | powerPaint.setStyle(Paint.Style.FILL); 158 | powerPaint.setColor(highColor); 159 | powerPaint.setStrokeWidth(0); 160 | // 电池头部 161 | headerPaint = new Paint(); 162 | headerPaint.setAntiAlias(true); 163 | headerPaint.setStyle(Paint.Style.FILL); 164 | headerPaint.setColor(headerColor); 165 | headerPaint.setStrokeWidth(0); 166 | } 167 | 168 | @Override 169 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 170 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 171 | width = getMeasuredWidth(); 172 | height = getMeasuredHeight(); 173 | } 174 | 175 | private RectF borderRf; 176 | private RectF powerRf; 177 | private RectF headerRf; 178 | 179 | @Override 180 | protected void onDraw(Canvas canvas) { 181 | if(orientation == BatteryViewOrientation.HORIZONTAL_LEFT){ 182 | drawHorizontalLeft(canvas); 183 | }else if(orientation == BatteryViewOrientation.HORIZONTAL_RIGHT){ 184 | drawHorizontalRight(canvas); 185 | }else if (orientation == BatteryViewOrientation.VERTICAL_TOP){ 186 | drawVerticalTop(canvas); 187 | }else if (orientation == BatteryViewOrientation.VERTICAL_BOTTOM){ 188 | drawVerticalBottom(canvas); 189 | } 190 | } 191 | 192 | // 朝左的电池 193 | private void drawHorizontalLeft(Canvas canvas) { 194 | // 绘制边框 195 | if (borderRf == null) { 196 | borderRf = new RectF(headerWidth+borderPadding+borderWidth, borderWidth, width - borderWidth, height - borderWidth); 197 | } 198 | canvas.drawRoundRect(borderRf, radis, radis, borderPaint); 199 | // 绘制实心区域 200 | if (powerRf == null) { 201 | initBattery(); 202 | setPower(currentPower); 203 | } 204 | canvas.drawRoundRect(powerRf, radis, radis, powerPaint); 205 | 206 | // 绘制电池头 207 | if (headerRf == null) { 208 | float headerHeight = height / 3f; 209 | headerRf = new RectF(0, headerHeight, headerWidth, headerHeight * 2); 210 | } 211 | canvas.drawRoundRect(headerRf, radis, radis, headerPaint); 212 | } 213 | // 朝右的电池 214 | private void drawHorizontalRight(Canvas canvas) { 215 | // 绘制边框 216 | if (borderRf == null) { 217 | borderRf = new RectF(borderWidth, borderWidth, width - borderWidth - borderPadding - headerWidth, height - borderWidth); 218 | } 219 | canvas.drawRoundRect(borderRf, radis, radis, borderPaint); 220 | // 绘制实心区域 221 | if (powerRf == null) { 222 | initBattery(); 223 | setPower(currentPower); 224 | } 225 | canvas.drawRoundRect(powerRf, radis, radis, powerPaint); 226 | 227 | // 绘制电池头 228 | if (headerRf == null) { 229 | float headerHeight = height / 3f; 230 | headerRf = new RectF(width - headerWidth, headerHeight, width, headerHeight * 2); 231 | } 232 | canvas.drawRoundRect(headerRf, radis, radis, headerPaint); 233 | } 234 | // 电池横向的宽度 235 | private float getHorizontalWidth(int power) { 236 | // 满电量宽度 237 | float fullWidth = width - borderWidth * 2 - borderPadding * 2 - borderPadding - headerWidth; 238 | return fullWidth * power / 100f; 239 | } 240 | 241 | // 电池头朝上 242 | private void drawVerticalTop(Canvas canvas) { 243 | // 绘制边框 244 | if (borderRf == null) { 245 | borderRf = new RectF(borderWidth, headerWidth + borderPadding + borderWidth, width - borderWidth, height - borderWidth); 246 | } 247 | canvas.drawRoundRect(borderRf, radis, radis, borderPaint); 248 | // 绘制实心区域 249 | if (powerRf == null) { 250 | initBattery(); 251 | setPower(currentPower); 252 | } 253 | canvas.drawRoundRect(powerRf, radis, radis, powerPaint); 254 | 255 | // 绘制电池头 256 | if (headerRf == null) { 257 | float headerWidth1 = width / 3f; 258 | headerRf = new RectF(headerWidth1, 0, headerWidth1 * 2, headerWidth); 259 | } 260 | canvas.drawRoundRect(headerRf, radis, radis, headerPaint); 261 | } 262 | 263 | // 电池头朝下 264 | private void drawVerticalBottom(Canvas canvas) { 265 | // 绘制边框 266 | if (borderRf == null) { 267 | borderRf = new RectF(borderWidth, borderWidth, width - borderWidth, height - headerWidth - borderPadding - borderWidth); 268 | } 269 | canvas.drawRoundRect(borderRf, radis, radis, borderPaint); 270 | // 绘制实心区域 271 | if (powerRf == null) { 272 | initBattery(); 273 | setPower(currentPower); 274 | } 275 | canvas.drawRoundRect(powerRf, radis, radis, powerPaint); 276 | 277 | // 绘制电池头 278 | if (headerRf == null) { 279 | float headerWidth1 = width / 3f; 280 | headerRf = new RectF(headerWidth1, height - headerWidth, headerWidth1 * 2, height); 281 | } 282 | canvas.drawRoundRect(headerRf, radis, radis, headerPaint); 283 | } 284 | 285 | private float getVerticalHeight(int power){ 286 | // 满电量宽度 287 | float fullHeight = height - borderWidth * 2 - borderPadding * 3 - headerWidth; 288 | return fullHeight * power / 100f; 289 | } 290 | 291 | public void setPower(int power) { 292 | if(mOnBatteryPowerListener != null) mOnBatteryPowerListener.onPower(currentPower); 293 | 294 | if (power <= lowValue) { 295 | powerPaint.setColor(lowColor); 296 | } else if (power < mediumValue) { 297 | powerPaint.setColor(mediumColor); 298 | } else { 299 | if(runnable == null){ 300 | powerPaint.setColor(noChargingHighColor); 301 | }else{ 302 | powerPaint.setColor(highColor); 303 | } 304 | } 305 | 306 | if(orientation == BatteryViewOrientation.HORIZONTAL_RIGHT){ 307 | float realWidth = getHorizontalWidth(power); 308 | powerRf = new RectF(borderWidth + borderPadding, borderWidth + borderPadding, borderWidth + borderPadding + realWidth, height - borderWidth - borderPadding); 309 | postInvalidate(); 310 | }else if(orientation == BatteryViewOrientation.HORIZONTAL_LEFT){ 311 | float realWidth = getHorizontalWidth(power); 312 | powerRf = new RectF(width-borderWidth-borderPadding-realWidth, borderWidth + borderPadding, width - borderWidth - borderPadding, height - borderWidth - borderPadding); 313 | postInvalidate(); 314 | } else if(orientation == BatteryViewOrientation.VERTICAL_TOP){ 315 | float realHeight = getVerticalHeight(power); 316 | powerRf = new RectF(borderWidth + borderPadding, height - borderWidth - borderPadding - realHeight, width - borderWidth - borderPadding, height - borderWidth - borderPadding); 317 | postInvalidate(); 318 | }else if(orientation == BatteryViewOrientation.VERTICAL_BOTTOM){ 319 | float realHeight = getVerticalHeight(power); 320 | powerRf = new RectF(borderWidth + borderPadding, borderWidth + borderPadding, width - borderWidth - borderPadding, borderWidth + borderPadding + realHeight); 321 | postInvalidate(); 322 | } 323 | } 324 | 325 | 326 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) 327 | private void registerPower() { 328 | IntentFilter filter = new IntentFilter(); 329 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); 330 | mContext.registerReceiver(receiver, filter); 331 | } 332 | 333 | private BroadcastReceiver receiver = new BroadcastReceiver() { 334 | @Override 335 | public void onReceive(Context context, Intent intent) { 336 | // 用getIntExtra("level")和getIntExtra("scale")获得相应值 337 | int power = intent.getIntExtra("level", 0); ///电池剩余电量 338 | Log.i("电池剩余电量power", power + ""); 339 | int scale = intent.getIntExtra("scale", 0); ///获取电池满电量数值 340 | // Log.i("获取电池满电量数值scale", scale+""); 341 | // String technology = intent.getStringExtra("technology"); ///获取电池技术支持 342 | // Log.i("获取电池技术支持technology", technology + ""); 343 | int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); ///获取电池状态 344 | // Log.i("获取电池状态status", status + ""); 345 | // int plugged = intent.getIntExtra("plugged", 0); ///获取电源信息 346 | // Log.i("获取电源信息plugged", plugged + ""); 347 | // int health = intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN); ///获取电池健康度 348 | // Log.i("获取电池健康度health", health + ""); 349 | // int voltage = intent.getIntExtra("voltage", 0); ///获取电池电压 350 | // Log.i("获取电池电压voltage", voltage + ""); 351 | // int temperature = intent.getIntExtra("temperature", 0); ///获取电池温度 352 | // Log.i("获取电池温度temperature", temperature + ""); 353 | if (status == BatteryManager.BATTERY_STATUS_CHARGING && power < 100) { 354 | startCharge(); 355 | } else { 356 | stopCharge(); 357 | } 358 | 359 | currentPower = power; 360 | if (powerRf != null && runnable == null) { 361 | setPower(power); 362 | } 363 | } 364 | }; 365 | 366 | @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) 367 | private void unregisterPower() { 368 | mContext.unregisterReceiver(receiver); 369 | } 370 | 371 | private int power; 372 | 373 | // 充电动态显示 374 | private void startCharge() { 375 | if (runnable != null) return; 376 | power = currentPower; 377 | runnable = new Runnable() { 378 | @Override 379 | public void run() { 380 | power %= 100; 381 | setPower(power); 382 | power += chargingSpeed; 383 | //延迟执行 384 | mHandler.postDelayed(this, 200); 385 | } 386 | }; 387 | mHandler.post(runnable); 388 | } 389 | 390 | private void stopCharge() { 391 | if (runnable != null) { 392 | mHandler.removeCallbacks(runnable); 393 | runnable = null; 394 | } 395 | } 396 | 397 | // private 398 | private Lifecycle mLifecycle; 399 | private OnBatteryPowerListener mOnBatteryPowerListener; 400 | 401 | // 公用方法 402 | public void setLifecycleOwner(@NonNull LifecycleOwner owner) { 403 | if (mLifecycle != null) mLifecycle.removeObserver(this); 404 | mLifecycle = owner.getLifecycle(); 405 | mLifecycle.addObserver(this); 406 | } 407 | 408 | public void setOnBatteryPowerListener(OnBatteryPowerListener onBatteryPowerListener){ 409 | mOnBatteryPowerListener = onBatteryPowerListener; 410 | } 411 | public void removeOnBatteryPowerListener(){ 412 | mOnBatteryPowerListener = null; 413 | } 414 | 415 | /** 416 | * 充电动画速度 417 | * @param speed 1-9之前的数值 418 | */ 419 | public void setChargingSpeed(int speed){ 420 | this.chargingSpeed = speed; 421 | } 422 | 423 | } 424 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/java/com/sjx/batteryviewlibrary/BatteryViewOrientation.java: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryviewlibrary; 2 | 3 | public enum BatteryViewOrientation { 4 | HORIZONTAL_LEFT, 5 | HORIZONTAL_RIGHT, 6 | VERTICAL_TOP, 7 | VERTICAL_BOTTOM 8 | } 9 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/java/com/sjx/batteryviewlibrary/OnBatteryPowerListener.java: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryviewlibrary; 2 | 3 | public interface OnBatteryPowerListener { 4 | void onPower(int power); 5 | } 6 | -------------------------------------------------------------------------------- /batteryviewlibrary/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 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #CC0000 4 | #FFCC00 5 | #00CC00 6 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | batteryviewlibrary 3 | 4 | -------------------------------------------------------------------------------- /batteryviewlibrary/src/test/java/com/sjx/batteryviewlibrary/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.sjx.batteryviewlibrary 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | ext.kotlin_version = '1.3.50' 5 | repositories { 6 | google() 7 | jcenter() 8 | 9 | } 10 | dependencies { 11 | classpath 'com.android.tools.build:gradle:3.5.3' 12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | 16 | 17 | //添加下面这句 18 | classpath 'com.novoda:bintray-release:0.9.1' 19 | } 20 | } 21 | 22 | allprojects { 23 | repositories { 24 | google() 25 | jcenter() 26 | 27 | } 28 | } 29 | 30 | task clean(type: Delete) { 31 | delete rootProject.buildDir 32 | } 33 | -------------------------------------------------------------------------------- /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 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app's APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true 20 | # Kotlin code style for this project: "official" or "obsolete": 21 | kotlin.code.style=official 22 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Dec 18 09:24:33 CST 2019 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-5.4.1-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 | -------------------------------------------------------------------------------- /imgs/battery.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swallowsonny/BatteryView/173e5010612af8212e82b9a99df74436c7aa1479/imgs/battery.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':batteryviewlibrary' 2 | rootProject.name='BatteryView' 3 | --------------------------------------------------------------------------------