feat: 优化代码

pull/141/head
kunfei 5 years ago
parent 25d16ffa0d
commit 73e01378bf
  1. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 9
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  3. 50
      app/src/main/java/io/legado/app/ui/widget/BatteryView.kt
  4. 6
      app/src/main/res/layout/view_book_page.xml

@ -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
}

@ -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)
}
}

@ -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)
}
}

@ -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">
<TextView
@ -67,6 +68,11 @@
android:layout_height="wrap_content"
android:textSize="12sp" />
<io.legado.app.ui.widget.BatteryView
android:id="@+id/battery_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
Loading…
Cancel
Save