pull/142/head
yangyxd 5 years ago
commit 758092ea23
  1. 1
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt
  3. 68
      app/src/main/java/io/legado/app/ui/widget/BatteryView.kt
  4. 3
      app/src/main/res/layout/view_book_page.xml

@ -75,6 +75,7 @@ object ChapterProvider {
item.chapterIndex = bookChapter.index item.chapterIndex = bookChapter.index
item.chapterSize = chapterSize item.chapterSize = chapterSize
item.title = bookChapter.title item.title = bookChapter.title
item.upLinesPosition(visibleHeight)
} }
return TextChapter( return TextChapter(
bookChapter.index, bookChapter.index,

@ -18,6 +18,21 @@ data class TextPage(
var height: Float = 0f 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)
if (surplus == 0f) return
height += surplus
val tj = surplus / (textLines.size - 1)
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") @Suppress("DEPRECATION")
fun format(): TextPage { fun format(): TextPage {
if (textLines.isEmpty() && ChapterProvider.visibleWidth > 0) { if (textLines.isEmpty() && ChapterProvider.visibleWidth > 0) {
@ -101,4 +116,5 @@ data class TextPage(
} }
return percent return percent
} }
} }

@ -1,67 +1,57 @@
package io.legado.app.ui.widget package io.legado.app.ui.widget
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
import android.graphics.Rect import android.graphics.Rect
import android.graphics.Typeface
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import io.legado.app.R import androidx.appcompat.widget.AppCompatTextView
import io.legado.app.utils.dp 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) { class BatteryView(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
private var battery = 100 private val batteryPaint = Paint()
private val textPaint = TextPaint()
private var batteryHeight: Int = 0
private var batteryWidth: Int = 0
private val outFrame = Rect() private val outFrame = Rect()
private val polar = Rect() private val polar = Rect()
init { init {
textPaint.textSize = 10.sp.toFloat() setPadding(4.dp, 0, 6.dp, 0)
textPaint.strokeWidth = 1.dp.toFloat() batteryPaint.strokeWidth = 1.dp.toFloat()
textPaint.isAntiAlias = true batteryPaint.isAntiAlias = true
textPaint.textAlign = Paint.Align.CENTER batteryPaint.color = paint.color
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)
)
} }
fun setColor(@ColorInt color: Int) { fun setColor(@ColorInt color: Int) {
textPaint.color = color setTextColor(color)
batteryPaint.color = color
invalidate() invalidate()
} }
@SuppressLint("SetTextI18n")
fun setBattery(battery: Int) { fun setBattery(battery: Int) {
this.battery = battery text = "$battery"
invalidate()
} }
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
textPaint.style = Paint.Style.STROKE outFrame.set(
canvas.drawRect(outFrame, textPaint) 1.dp,
textPaint.style = Paint.Style.FILL layout.getLineTop(0) + 2.dp,
canvas.drawRect(polar, textPaint) width - 3.dp,
val text = battery.toString() layout.getLineBottom(0) - 2.dp
val baseHeight = batteryHeight - textPaint.fontMetrics.descent )
canvas.drawText(text, outFrame.right / 2.toFloat(), baseHeight, textPaint) 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)
} }
} }

@ -78,7 +78,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="3dp" android:layout_marginLeft="3dp"
tools:ignore="RtlHardcoded,RtlSymmetry" /> android:textSize="10sp"
tools:ignore="RtlHardcoded,RtlSymmetry,SmallSp" />
</LinearLayout> </LinearLayout>

Loading…
Cancel
Save