自定義View
自定義電池電量的 View
如下:
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
class BatteryView : View {
private var mMaxLevel = 100
private var mLinePaint: Paint? = null
private var mBatteryPaint: Paint? = null
private var mRectPaint: Paint? = null
private var mTextPaint: Paint? = null
private var mRectF: RectF? = null
private var mWidth = 0
private var mHeight = 0
private var mPerPartWidth = 0
private var mBatteryLevel = 0
private var mHeaderHeight = 0 //電池頭部高度
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
mWidth = MeasureSpec.getSize(widthMeasureSpec)
mHeight = MeasureSpec.getSize(heightMeasureSpec)
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED
|| MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST
) {
mWidth = 150
}
if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED
|| MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST
) {
mHeight = 80
}
mPerPartWidth = (mWidth - BATTERY_HEADER_WIDTH) / PART_COUNT
mHeaderHeight = mHeight / 3
setMeasuredDimension(mWidth, mHeight)
}
private fun init() {
mLinePaint = Paint()
mLinePaint!!.isAntiAlias = true
mLinePaint!!.color = Color.WHITE
mBatteryPaint = Paint()
mBatteryPaint!!.isAntiAlias = true
mBatteryPaint!!.color = Color.WHITE
mRectPaint = Paint()
mRectPaint!!.isAntiAlias = true
mRectPaint!!.color = Color.GRAY
mRectPaint!!.style = Paint.Style.FILL
mTextPaint = Paint()
mTextPaint!!.color = Color.BLACK
mTextPaint!!.textSize = 50f
mTextPaint!!.style = Paint.Style.FILL
mTextPaint!!.textAlign = Paint.Align.CENTER
mRectF = RectF()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val batteryWidth = mWidth - BATTERY_HEADER_WIDTH
//繪制電池背景
mRectF!!.right = batteryWidth.toFloat()
mRectF!!.bottom = mHeight.toFloat()
canvas.drawRoundRect(mRectF!!, 20f, 20f, mRectPaint!!)
//繪制當(dāng)前電量
canvas.save()
canvas.clipRect(0, 0, batteryWidth * mBatteryLevel / mMaxLevel, mHeight) //裁剪矩形
canvas.drawRoundRect(mRectF!!, 20f, 20f, mBatteryPaint!!)
canvas.restore()
if (DRAW_PART_LINE) {
//繪制電池分格線
for (i in 1 until PART_COUNT) {
canvas.drawLine(
(mPerPartWidth * i).toFloat(),
0f,
(mPerPartWidth * i).toFloat(),
mHeight.toFloat(),
mLinePaint!!
)
}
}
//繪制電量文字
val fontMetrics = mTextPaint!!.fontMetrics
val top = fontMetrics.top //基線到字體上邊框的距離
val bottom = fontMetrics.bottom //基線到字體下邊框的距離
val baseLineY = (mRectF!!.centerY() - top / 2 - bottom / 2).toInt() //基線中間點(diǎn)的y軸
canvas.drawText(
mBatteryLevel.toString(),
mRectF!!.centerX(),
baseLineY.toFloat(),
mTextPaint!!
)
//繪制右邊電池頭部
mRectF!!.left = batteryWidth.toFloat()
mRectF!!.top = (mHeight / 2 - mHeaderHeight / 2).toFloat()
mRectF!!.right = (mRectF!!.left + BATTERY_HEADER_WIDTH).toFloat()
mRectF!!.bottom = (mHeight / 2 + mHeaderHeight / 2).toFloat()
mRectPaint!!.style = Paint.Style.FILL
canvas.drawRect(mRectF!!, mRectPaint!!)
}
fun setBatteryLevel(level: Int) {
mBatteryLevel = level
if (level <= 10) {
mBatteryPaint!!.color = Color.RED
} else {
mBatteryPaint!!.color = Color.WHITE
}
postInvalidate()
}
fun setMaxLevel(maxLevel: Int) {
mMaxLevel = maxLevel
postInvalidate()
}
companion object {
private const val DRAW_PART_LINE = false //是否繪制分格線
private const val PART_COUNT = 4 //分格總數(shù)
private const val BATTERY_HEADER_WIDTH = 8 //右邊電池頭寬度
}
}
自定義中通過 Canvas.clipRect()
函數(shù)裁剪圓角矩形,實(shí)現(xiàn)類似蘋果電量的半圓角半直角的效果。
電量監(jiān)聽
主界面代碼監(jiān)聽電量變化的廣播:
class MainActivity : AppCompatActivity() {
private var mBatteryView: BatteryView? = null
protected fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fullscreen)
mBatteryView = findViewById(R.id.battery_view)
val filter = IntentFilter()
filter.addAction(Intent.ACTION_BATTERY_CHANGED)
registerReceiver(receiver, filter)
}
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action!! == Intent.ACTION_BATTERY_CHANGED) {
//當(dāng)前電量
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
//最大電池電量
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100)
//電池狀態(tài)
val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 100)
mBatteryView!!.setBatteryLevel(level)
}
}
}
}
protected fun onDestroy() {
unregisterReceiver(receiver)
super.onDestroy()
}
}
其中 BatteryManager.EXTRA_LEVEL
為當(dāng)前電量;BatteryManager.EXTRA_SCALE
為電池容量即最大電量(基本都為100);BatteryManager.EXTRA_STATUS
為電池狀態(tài),以此判斷是否在充電狀態(tài)。文章來源:http://www.zghlxwxcb.cn/news/detail-516892.html
效果
電量100%時顯示如下:
小于等于10%為低電量,顯示預(yù)警色(可根據(jù)需要調(diào)節(jié)低電量閾值):
電量20%:
可通過 DRAW_PART_LINE 的布爾常量設(shè)置是否要分格并顯示分格線:
上述內(nèi)容默認(rèn)最大電量為100,可通過 setMaxLevel()
方法設(shè)置最大電池容量。文章來源地址http://www.zghlxwxcb.cn/news/detail-516892.html
到了這里,關(guān)于Android仿蘋果電量顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!