pull/34/head
kunfei 5 years ago
parent c3166a16f4
commit 48cf1c7e6d
  1. 6
      app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt
  2. 4
      app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt
  3. 17
      app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt
  4. 10
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  5. 12
      app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt

@ -18,6 +18,7 @@ object ChapterProvider {
content: String, chapterSize: Int content: String, chapterSize: Int
): TextChapter { ): TextChapter {
val textPages = arrayListOf<TextPage>() val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>() val pageLengths = arrayListOf<Int>()
var surplusText = content var surplusText = content
var pageIndex = 0 var pageIndex = 0
@ -35,10 +36,12 @@ object ChapterProvider {
} }
} }
textView.text = spannableStringBuilder textView.text = spannableStringBuilder
val lastCharNum = textView.getCharNum() val lastLine = textView.getLineNum()
val lastCharNum = textView.getCharNum(lastLine)
if (lastCharNum == 0) { if (lastCharNum == 0) {
break break
} else { } else {
pageLines.add(lastLine)
pageLengths.add(lastCharNum) pageLengths.add(lastCharNum)
textPages.add( textPages.add(
TextPage( TextPage(
@ -64,6 +67,7 @@ object ChapterProvider {
bookChapter.title, bookChapter.title,
bookChapter.url, bookChapter.url,
textPages, textPages,
pageLines,
pageLengths, pageLengths,
chapterSize chapterSize
) )

@ -16,8 +16,8 @@ class ContentTextView : AppCompatTextView {
/** /**
* 获取当前页总字数 * 获取当前页总字数
*/ */
fun getCharNum(): Int { fun getCharNum(lineNum: Int = getLineNum()): Int {
return layout?.getLineEnd(getLineNum()) ?: 0 return layout?.getLineEnd(lineNum) ?: 0
} }
/** /**

@ -13,7 +13,14 @@ import io.legado.app.constant.AppConst.TIME_FORMAT
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.android.synthetic.main.view_book_page.view.* import kotlinx.android.synthetic.main.view_book_page.view.content_text_view
import kotlinx.android.synthetic.main.view_book_page.view.page_panel
import kotlinx.android.synthetic.main.view_book_page.view.top_bar
import kotlinx.android.synthetic.main.view_book_page.view.tv_bottom_left
import kotlinx.android.synthetic.main.view_book_page.view.tv_bottom_right
import kotlinx.android.synthetic.main.view_book_page.view.tv_top_left
import kotlinx.android.synthetic.main.view_book_page.view.tv_top_right
import kotlinx.android.synthetic.main.view_book_page_scroll.view.*
import org.jetbrains.anko.matchParent import org.jetbrains.anko.matchParent
import java.util.* import java.util.*
@ -111,4 +118,12 @@ class ContentView : FrameLayout {
fun contentTextView(): ContentTextView? { fun contentTextView(): ContentTextView? {
return content_text_view return content_text_view
} }
fun scrollTo(pos: Int?) {
if (pos != null) {
page_scroll_view?.post {
page_scroll_view?.scrollTo(0, content_text_view.layout.getLineTop(pos))
}
}
}
} }

@ -34,10 +34,8 @@ class PageView(context: Context, attrs: AttributeSet) :
addView(curPage) addView(curPage)
upBg() upBg()
setWillNotDraw(false) setWillNotDraw(false)
pageFactory = TextPageFactory.create(this)
upPageAnim() upPageAnim()
this.pageFactory = TextPageFactory.create(this)
upContent()
} }
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
@ -88,6 +86,7 @@ class PageView(context: Context, attrs: AttributeSet) :
2 -> ScrollPageDelegate(this) 2 -> ScrollPageDelegate(this)
else -> NoAnimPageDelegate(this) else -> NoAnimPageDelegate(this)
} }
upContent()
} }
fun upContent() { fun upContent() {
@ -96,6 +95,11 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage?.setContent(it.currentPage()) curPage?.setContent(it.currentPage())
nextPage?.setContent(it.nextPage()) nextPage?.setContent(it.nextPage())
} }
callback?.let {
if (isScrollDelegate()) {
curPage?.scrollTo(it.textChapter()?.getStartLine(it.durChapterPos()))
}
}
} }
fun moveToPrevPage(noAnim: Boolean = true) { fun moveToPrevPage(noAnim: Boolean = true) {

@ -7,6 +7,7 @@ data class TextChapter(
val title: String, val title: String,
val url: String, val url: String,
val pages: List<TextPage>, val pages: List<TextPage>,
val pageLines: List<Int>,
val pageLengths: List<Int>, val pageLengths: List<Int>,
val chaptersSize: Int val chaptersSize: Int
) { ) {
@ -64,5 +65,16 @@ data class TextChapter(
} }
return stringBuilder.toString() return stringBuilder.toString()
} }
fun getStartLine(pageIndex: Int): Int {
if (pageLines.size > pageIndex) {
var lines = 0
for (index: Int in 0 until pageIndex) {
lines += pageLines[index] + 1
}
return lines
}
return 0
}
} }

Loading…
Cancel
Save