From 73e01378bf074d2384f6d48445fb4220539148d3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 8 Mar 2020 19:04:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/page/ChapterProvider.kt | 2 +- .../app/ui/book/read/page/ContentView.kt | 9 +++- .../io/legado/app/ui/widget/BatteryView.kt | 50 +++++++++++++++++++ app/src/main/res/layout/view_book_page.xml | 6 +++ 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/BatteryView.kt diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt index dbd251607..65121906c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt @@ -294,5 +294,5 @@ object ChapterProvider { } val TextPaint.textHeight: Float - get() = this.fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading + get() = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index 534ee75d2..389d924f6 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -71,7 +71,12 @@ class ContentView(context: Context) : FrameLayout(context) { } if (hideStatusBar) { tv_bottom_left.text = timeFormat.format(Date(System.currentTimeMillis())) - tv_bottom_right.text = context.getString(R.string.battery_show, battery) + tv_bottom_right.gone() + battery_view.visible() + battery_view.setBattery(battery) + } else { + tv_bottom_right.visible() + battery_view.gone() } } } @@ -98,7 +103,7 @@ class ContentView(context: Context) : FrameLayout(context) { fun upBattery(battery: Int) { this.battery = battery if (ReadBookConfig.hideStatusBar) { - tv_bottom_right.text = context.getString(R.string.battery_show, battery) + battery_view.setBattery(battery) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt new file mode 100644 index 000000000..74c1bda9f --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt @@ -0,0 +1,50 @@ +package io.legado.app.ui.widget + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Paint +import android.graphics.Rect +import android.graphics.Typeface +import android.text.StaticLayout +import android.text.TextPaint +import android.util.AttributeSet +import android.view.View +import io.legado.app.utils.dp + +class BatteryView(context: Context, attrs: AttributeSet?) : View(context, attrs) { + private var battery = 100 + private val textPaint = TextPaint() + private var batteryHeight: Int = 0 + private var batteryWidth: Int = 0 + private val outFrame = Rect() + private val polar = Rect() + + init { + textPaint.isAntiAlias = true + textPaint.textAlign = Paint.Align.CENTER + textPaint.typeface = Typeface.createFromAsset(context.assets, "number.ttf") + batteryHeight = with(textPaint.fontMetrics) { descent - ascent + leading }.toInt() + 4.dp + batteryWidth = StaticLayout.getDesiredWidth("100", textPaint).toInt() + 5.dp + outFrame.set(0, 0, batteryWidth - 2.dp, batteryHeight) + polar.set(outFrame.right, batteryHeight / 4, batteryWidth, batteryHeight * 3 / 4) + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(batteryWidth, batteryHeight) + } + + fun setBattery(battery: Int) { + this.battery = battery + invalidate() + } + + override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + canvas.drawRect(polar, textPaint) + textPaint.style = Paint.Style.STROKE + canvas.drawRect(outFrame, textPaint) + val text = battery.toString() + canvas.drawText(text, outFrame.right / 2.toFloat(), batteryHeight / 2.toFloat(), textPaint) + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/view_book_page.xml b/app/src/main/res/layout/view_book_page.xml index bba89c8fb..aa8168165 100644 --- a/app/src/main/res/layout/view_book_page.xml +++ b/app/src/main/res/layout/view_book_page.xml @@ -51,6 +51,7 @@ android:id="@+id/ll_footer" android:layout_width="match_parent" android:layout_height="wrap_content" + android:gravity="center_vertical" android:orientation="horizontal"> + + \ No newline at end of file