feat: 优化代码

pull/106/head
kunfei 5 years ago
parent 8b513c31fe
commit 891fdc1f8c
  1. 24
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt

@ -1,9 +1,11 @@
package io.legado.app.ui.book.read.page package io.legado.app.ui.book.read.page
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.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View import android.view.View
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
@ -38,6 +40,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
return true
}
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
textPage?.let { textPage -> textPage?.let { textPage ->
@ -73,4 +81,20 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
fun selectText(x: Float, y: Float) {
textPage?.let { textPage ->
for (textLine in textPage.textLines) {
if (y > textLine.lineTop && y < textLine.lineBottom) {
for (textChar in textLine.textChars) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
textChar.selected = true
invalidate()
break
}
}
break
}
}
}
}
} }

@ -18,11 +18,10 @@ import java.util.*
class ContentView : FrameLayout { class ContentView : FrameLayout {
var callBack: CallBack? = null var callBack: CallBack? = null
private var isScroll: Boolean = false var headerHeight = 0
private var pageSize: Int = 0 private var pageSize: Int = 0
constructor(context: Context) : super(context) { constructor(context: Context) : super(context) {
this.isScroll = true
init() init()
} }
@ -40,7 +39,7 @@ class ContentView : FrameLayout {
fun upStyle() { fun upStyle() {
ReadBookConfig.durConfig.apply { ReadBookConfig.durConfig.apply {
val rootPaddingTop = if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) { if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
//显示状态栏时隐藏header //显示状态栏时隐藏header
ll_header.visible() ll_header.visible()
ll_header.layoutParams = ll_header.layoutParams =
@ -51,12 +50,13 @@ class ContentView : FrameLayout {
headerPaddingRight.dp, headerPaddingRight.dp,
headerPaddingBottom.dp headerPaddingBottom.dp
) )
0 headerHeight = ll_header.height
page_panel.setPadding(0, 0, 0, 0)
} else { } else {
ll_header.gone() ll_header.gone()
context.getStatusBarHeight() headerHeight = context.getStatusBarHeight()
page_panel.setPadding(0, headerHeight, 0, 0)
} }
page_panel.setPadding(0, rootPaddingTop, 0, 0)
content_text_view.setPadding( content_text_view.setPadding(
paddingLeft.dp, paddingLeft.dp,
paddingTop.dp, paddingTop.dp,
@ -111,8 +111,8 @@ class ContentView : FrameLayout {
} }
fun selectText(e: MotionEvent) { fun selectText(e: MotionEvent) {
val y = e.y - headerHeight
content_text_view.selectText(e.x, y)
} }
fun scrollTo(pos: Int?) { fun scrollTo(pos: Int?) {

Loading…
Cancel
Save