From 48bfabcbeabf109cb4ea84abe754805056618618 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Mar 2020 09:36:34 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/widget/BatteryView.kt | 61 ++++++++----------- app/src/main/res/layout/view_book_page.xml | 3 +- 2 files changed, 26 insertions(+), 38 deletions(-) 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 index 60e781c93..1cae8e09d 100644 --- a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt @@ -1,67 +1,54 @@ package io.legado.app.ui.widget +import android.annotation.SuppressLint 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 androidx.annotation.ColorInt +import androidx.appcompat.widget.AppCompatTextView import io.legado.app.R import io.legado.app.utils.dp import io.legado.app.utils.getCompatColor -import io.legado.app.utils.sp -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 +class BatteryView(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) { + private val batteryPaint = Paint() private val outFrame = Rect() private val polar = Rect() init { - textPaint.textSize = 10.sp.toFloat() - textPaint.strokeWidth = 1.dp.toFloat() - textPaint.isAntiAlias = true - textPaint.textAlign = Paint.Align.CENTER - textPaint.color = context.getCompatColor(R.color.tv_text_default) - textPaint.typeface = Typeface.createFromAsset(context.assets, "number.ttf") - batteryHeight = with(textPaint.fontMetrics) { descent - ascent + leading }.toInt() - batteryWidth = StaticLayout.getDesiredWidth("100", textPaint).toInt() + 10.dp - outFrame.set(1.dp, 1.dp, batteryWidth - 3.dp, batteryHeight - 1.dp) - polar.set(outFrame.right, batteryHeight / 3, batteryWidth, batteryHeight * 2 / 3) - } - - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - super.onMeasure( - MeasureSpec.makeMeasureSpec(batteryWidth, MeasureSpec.EXACTLY), - MeasureSpec.makeMeasureSpec(batteryHeight, MeasureSpec.EXACTLY) - ) + setPadding(4.dp, 0, 6.dp, 0) + batteryPaint.strokeWidth = 1.dp.toFloat() + batteryPaint.isAntiAlias = true + batteryPaint.color = context.getCompatColor(R.color.tv_text_default) } fun setColor(@ColorInt color: Int) { - textPaint.color = color + setTextColor(color) + batteryPaint.color = color invalidate() } + @SuppressLint("SetTextI18n") fun setBattery(battery: Int) { - this.battery = battery - invalidate() + text = "$battery" } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - textPaint.style = Paint.Style.STROKE - canvas.drawRect(outFrame, textPaint) - textPaint.style = Paint.Style.FILL - canvas.drawRect(polar, textPaint) - val text = battery.toString() - val baseHeight = batteryHeight - textPaint.fontMetrics.descent - canvas.drawText(text, outFrame.right / 2.toFloat(), baseHeight, textPaint) + outFrame.set( + 1.dp, + layout.getLineTop(0) + 2.dp, + width - 3.dp, + layout.getLineBottom(0) - 2.dp + ) + val dj = (outFrame.bottom - outFrame.top) / 3 + polar.set(outFrame.right, outFrame.top + dj, width - 1.dp, outFrame.bottom - dj) + batteryPaint.style = Paint.Style.STROKE + canvas.drawRect(outFrame, batteryPaint) + batteryPaint.style = Paint.Style.FILL + canvas.drawRect(polar, batteryPaint) } } \ 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 fc80e05f5..922819b1e 100644 --- a/app/src/main/res/layout/view_book_page.xml +++ b/app/src/main/res/layout/view_book_page.xml @@ -78,7 +78,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" - tools:ignore="RtlHardcoded,RtlSymmetry" /> + android:textSize="10sp" + tools:ignore="RtlHardcoded,RtlSymmetry,SmallSp" /> From 6a5d38131467504615a69f467107e80e61f76fde Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Mar 2020 09:41:20 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/widget/BatteryView.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 index 1cae8e09d..a51e250f7 100644 --- a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt @@ -8,9 +8,7 @@ import android.graphics.Rect import android.util.AttributeSet import androidx.annotation.ColorInt import androidx.appcompat.widget.AppCompatTextView -import io.legado.app.R import io.legado.app.utils.dp -import io.legado.app.utils.getCompatColor class BatteryView(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) { private val batteryPaint = Paint() @@ -21,7 +19,7 @@ class BatteryView(context: Context, attrs: AttributeSet?) : AppCompatTextView(co setPadding(4.dp, 0, 6.dp, 0) batteryPaint.strokeWidth = 1.dp.toFloat() batteryPaint.isAntiAlias = true - batteryPaint.color = context.getCompatColor(R.color.tv_text_default) + batteryPaint.color = paint.color } fun setColor(@ColorInt color: Int) { From 19e2fa92c92ef9ca4366a33b94288910676f8eef Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Mar 2020 09:42:33 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/widget/BatteryView.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 index a51e250f7..f11db64e8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/BatteryView.kt @@ -42,7 +42,12 @@ class BatteryView(context: Context, attrs: AttributeSet?) : AppCompatTextView(co layout.getLineBottom(0) - 2.dp ) val dj = (outFrame.bottom - outFrame.top) / 3 - polar.set(outFrame.right, outFrame.top + dj, width - 1.dp, outFrame.bottom - dj) + polar.set( + outFrame.right, + outFrame.top + dj, + width - 1.dp, + outFrame.bottom - dj + ) batteryPaint.style = Paint.Style.STROKE canvas.drawRect(outFrame, batteryPaint) batteryPaint.style = Paint.Style.FILL From 554c388b52616a1add581ad4ab910805e928c534 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Mar 2020 11:52:08 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=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 | 1 + .../app/ui/book/read/page/entities/TextPage.kt | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) 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 65121906c..b7cf7182e 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 @@ -75,6 +75,7 @@ object ChapterProvider { item.chapterIndex = bookChapter.index item.chapterSize = chapterSize item.title = bookChapter.title + item.upLinesPosition(visibleHeight) } return TextChapter( bookChapter.index, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index bfb8e822c..adb5f0ec9 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -18,6 +18,21 @@ data class TextPage( var height: Float = 0f ) { + fun upLinesPosition(visibleHeight: Int) { + if (textLines.size <= 1) return + if (visibleHeight - height > with(textLines.last()) { lineBottom - lineTop }) return + val surplus = (visibleHeight - textLines.last().lineBottom) + height += surplus + val tj = surplus / (textLines.size - 1) + if (tj == 0f) return + for (i in 1 until textLines.size) { + val line = textLines[i] + line.lineTop = line.lineTop + tj * i + line.lineBase = line.lineBase + tj * i + line.lineBottom = line.lineBottom + tj * i + } + } + @Suppress("DEPRECATION") fun format(): TextPage { if (textLines.isEmpty() && ChapterProvider.visibleWidth > 0) { @@ -101,4 +116,5 @@ data class TextPage( } return percent } + } From 3cf9fb63449095b4c49c36783c16ee4c26bda827 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 9 Mar 2020 12:39:17 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/ui/book/read/page/entities/TextPage.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index adb5f0ec9..6c4e27073 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -22,9 +22,9 @@ data class TextPage( if (textLines.size <= 1) return if (visibleHeight - height > with(textLines.last()) { lineBottom - lineTop }) return val surplus = (visibleHeight - textLines.last().lineBottom) + if (surplus == 0f) return height += surplus val tj = surplus / (textLines.size - 1) - if (tj == 0f) return for (i in 1 until textLines.size) { val line = textLines[i] line.lineTop = line.lineTop + tj * i