feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 79293667db
commit cf8c7f216b
  1. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 6
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  3. 14
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  4. 22
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/ScrollPageDelegate.kt

@ -80,7 +80,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null
override val headerHeight: Int get() = page_view.headerHeight
override val headerHeight: Int get() = page_view.curPage.headerHeight
override fun onActivityCreated(savedInstanceState: Bundle?) {
Help.upLayoutInDisplayCutoutMode(window)
@ -323,11 +323,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
when (event.action) {
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
when (v.id) {
R.id.cursor_left -> page_view.selectStartMove(
R.id.cursor_left -> page_view.curPage.selectStartMove(
event.rawX + cursor_left.width,
event.rawY - cursor_left.height
)
R.id.cursor_right -> page_view.selectEndMove(
R.id.cursor_right -> page_view.curPage.selectEndMove(
event.rawX - cursor_right.width,
event.rawY - cursor_right.height
)
@ -385,7 +385,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
}
}
override val selectedText: String get() = page_view.selectedText
override val selectedText: String get() = page_view.curPage.selectedText
override fun onMenuItemSelected(item: MenuItemImpl): Boolean {
when (item.itemId) {
@ -395,6 +395,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
return false
}
override fun onMenuActionFinally() {
textActionMenu?.dismiss()
page_view.pageDelegate?.isTextSelected = false
}
/**
* 音量键翻页
*/

@ -70,11 +70,15 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
when (item.itemId) {
R.id.menu_copy -> context.sendToClip(callBack.selectedText)
}
callBack.onMenuActionFinally()
}
interface CallBack {
fun onMenuItemSelected(item: MenuItemImpl): Boolean
val selectedText: String
fun onMenuItemSelected(item: MenuItemImpl): Boolean
fun onMenuActionFinally()
}
}

@ -21,7 +21,7 @@ class PageView(context: Context, attrs: AttributeSet) :
var callBack: CallBack? = null
var pageFactory: TextPageFactory? = null
private var pageDelegate: PageDelegate? = null
var pageDelegate: PageDelegate? = null
var prevPage: ContentView
var curPage: ContentView
@ -183,18 +183,6 @@ class PageView(context: Context, attrs: AttributeSet) :
nextPage.upBattery(battery)
}
fun selectStartMove(x: Float, y: Float) {
curPage.selectStartMove(x, y)
}
fun selectEndMove(x: Float, y: Float) {
curPage.selectEndMove(x, y)
}
val headerHeight: Int get() = curPage.headerHeight
val selectedText: String get() = curPage.selectedText
override val isScrollDelegate: Boolean
get() = pageDelegate is ScrollPageDelegate

@ -1,15 +1,12 @@
package io.legado.app.ui.book.read.page.delegate
import android.graphics.Canvas
import android.graphics.Matrix
import android.view.MotionEvent
import io.legado.app.ui.book.read.page.PageView
import kotlin.math.abs
class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
private val bitmapMatrix = Matrix()
override fun onAnimStart() {
if (!atTop && !atBottom) {
stopScroll()
@ -39,26 +36,7 @@ class ScrollPageDelegate(pageView: PageView) : PageDelegate(pageView) {
}
override fun onDraw(canvas: Canvas) {
if (atTop || atBottom) {
val offsetY = touchY - startY
if ((mDirection == Direction.NEXT && offsetY > 0)
|| (mDirection == Direction.PREV && offsetY < 0)
) return
val distanceY = if (offsetY > 0) offsetY - viewHeight else offsetY + viewHeight
if (atTop && mDirection == Direction.PREV) {
bitmap?.let {
bitmapMatrix.setTranslate(0.toFloat(), distanceY)
canvas.drawBitmap(it, bitmapMatrix, null)
}
} else if (atBottom && mDirection == Direction.NEXT) {
bitmap?.let {
bitmapMatrix.setTranslate(0.toFloat(), distanceY)
canvas.drawBitmap(it, bitmapMatrix, null)
}
}
}
}
override fun onAnimStop() {

Loading…
Cancel
Save