feat: 优化代码

pull/111/head
kunfei 5 years ago
parent dc25ffa3dc
commit 9b128ab5f4
  1. 8
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 14
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  3. 28
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt

@ -16,6 +16,7 @@ import io.legado.app.utils.getPrefBoolean
class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) { class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val maxScrollOffset = 100f
private val selectedPaint by lazy { private val selectedPaint by lazy {
Paint().apply { Paint().apply {
color = context.getCompatColor(R.color.btn_bg_press_2) color = context.getCompatColor(R.color.btn_bg_press_2)
@ -84,6 +85,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
fun onScroll(offset: Float) { fun onScroll(offset: Float) {
var mOffset = offset
if (offset > maxScrollOffset) {
mOffset = maxScrollOffset
} else if (offset < -maxScrollOffset) {
mOffset = -maxScrollOffset
}
} }

@ -135,20 +135,6 @@ class ContentView(context: Context) : FrameLayout(context) {
val selectedText: String get() = content_text_view.selectedText val selectedText: String get() = content_text_view.selectedText
fun scrollTo(pos: Int?) {
if (pos != null) {
content_text_view.post {
}
}
}
fun scrollToBottom() {
content_text_view.post {
}
}
interface CallBack { interface CallBack {
fun scrollToLine(line: Int) fun scrollToLine(line: Int)
fun scrollToLast() fun scrollToLast()

@ -75,16 +75,10 @@ class PageView(context: Context, attrs: AttributeSet) :
PageDelegate.Direction.PREV -> { PageDelegate.Direction.PREV -> {
pageFactory?.moveToPrevious() pageFactory?.moveToPrevious()
upContent() upContent()
if (isScrollDelegate) {
curPage.scrollToBottom()
}
} }
PageDelegate.Direction.NEXT -> { PageDelegate.Direction.NEXT -> {
pageFactory?.moveToNext() pageFactory?.moveToNext()
upContent() upContent()
if (isScrollDelegate) {
curPage.scrollTo(0)
}
} }
else -> Unit else -> Unit
} }
@ -112,27 +106,15 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage.setContent(it.currentPage()) curPage.setContent(it.currentPage())
nextPage.setContent(it.nextPage()) nextPage.setContent(it.nextPage())
prevPage.setContent(it.previousPage()) prevPage.setContent(it.previousPage())
if (isScrollDelegate) {
curPage.scrollTo(ReadBook.textChapter()?.getStartLine(ReadBook.durChapterPos()))
}
} }
} }
if (isScrollDelegate) {
prevPage.scrollToBottom()
}
} }
callBack?.screenOffTimerStart() callBack?.screenOffTimerStart()
} }
fun moveToPrevPage(noAnim: Boolean = true) { fun moveToPrevPage(noAnim: Boolean = true) {
if (noAnim) { if (noAnim) {
if (isScrollDelegate) { fillPage(PageDelegate.Direction.PREV)
ReadBook.textChapter()?.let {
curPage.scrollTo(it.getStartLine(pageIndex - 1))
}
} else {
fillPage(PageDelegate.Direction.PREV)
}
} else { } else {
pageDelegate?.start(PageDelegate.Direction.PREV) pageDelegate?.start(PageDelegate.Direction.PREV)
} }
@ -140,13 +122,7 @@ class PageView(context: Context, attrs: AttributeSet) :
fun moveToNextPage(noAnim: Boolean = true) { fun moveToNextPage(noAnim: Boolean = true) {
if (noAnim) { if (noAnim) {
if (isScrollDelegate) { fillPage(PageDelegate.Direction.NEXT)
ReadBook.textChapter()?.let {
curPage.scrollTo(it.getStartLine(pageIndex + 1))
}
} else {
fillPage(PageDelegate.Direction.NEXT)
}
} else { } else {
pageDelegate?.start(PageDelegate.Direction.NEXT) pageDelegate?.start(PageDelegate.Direction.NEXT)
} }

Loading…
Cancel
Save