feat: 优化代码

pull/115/head
kunfei 5 years ago
parent fdb68976a4
commit 2ee5376abc
  1. 6
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  2. 102
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 6
      app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt
  4. 12
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  5. 9
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt

@ -119,12 +119,12 @@ object ChapterProvider {
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true
) )
for (lineIndex in 0 until layout.lineCount) { for (lineIndex in 0 until layout.lineCount) {
textPages.last().height = durY
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)
val textLine = TextLine(isTitle = true) val textLine = TextLine(isTitle = true)
if (durY < visibleHeight) { if (durY < visibleHeight) {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} else { } else {
textPages.last().height = durY
textPages.last().text = stringBuilder.toString() textPages.last().text = stringBuilder.toString()
stringBuilder.clear() stringBuilder.clear()
pageLines.add(textPages.last().textLines.size) pageLines.add(textPages.last().textLines.size)
@ -202,12 +202,12 @@ object ChapterProvider {
Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true Layout.Alignment.ALIGN_NORMAL, 1f, lineSpacingExtra, true
) )
for (lineIndex in 0 until layout.lineCount) { for (lineIndex in 0 until layout.lineCount) {
val textLine = TextLine(isTitle = false) textPages.last().height = durY
durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex) durY = durY + layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)
val textLine = TextLine()
if (durY < visibleHeight) { if (durY < visibleHeight) {
textPages.last().textLines.add(textLine) textPages.last().textLines.add(textLine)
} else { } else {
textPages.last().height = durY
textPages.last().text = stringBuilder.toString() textPages.last().text = stringBuilder.toString()
stringBuilder.clear() stringBuilder.clear()
pageLines.add(textPages.last().textLines.size) pageLines.add(textPages.last().textLines.size)

@ -77,7 +77,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
ReadBookConfig.durConfig.textColor() ReadBookConfig.durConfig.textColor()
} }
textLine.textChars.forEach { textLine.textChars.forEach {
canvas.drawText(it.charData, it.start, textLine.lineBase, textPaint) canvas.drawText(
it.charData,
it.start,
textLine.lineBase,
textPaint
)
if (it.selected) { if (it.selected) {
canvas.drawRect( canvas.drawRect(
it.start, it.start,
@ -92,11 +97,101 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
private fun drawScrollPage(canvas: Canvas) { private fun drawScrollPage(canvas: Canvas) {
if (pageOffset <= 0) {
textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint
} else {
ChapterProvider.contentPaint
}
textPaint.color = if (textLine.isReadAloud) {
context.accentColor
} else {
ReadBookConfig.durConfig.textColor()
}
textLine.textChars.forEach {
canvas.drawText(
it.charData,
it.start,
textLine.lineBase + pageOffset,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.start,
textLine.lineTop + pageOffset,
it.end,
textLine.lineBottom + pageOffset,
selectedPaint
)
}
}
}
pageFactory.nextPage?.textLines?.forEach { textLine ->
val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint
} else {
ChapterProvider.contentPaint
}
textPaint.color = if (textLine.isReadAloud) {
context.accentColor
} else {
ReadBookConfig.durConfig.textColor()
}
textLine.textChars.forEach {
canvas.drawText(
it.charData,
it.start,
textLine.lineBase + pageOffset + textPage.height - ChapterProvider.paddingTop,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.start,
textLine.lineTop + pageOffset + textPage.height - ChapterProvider.paddingTop,
it.end,
textLine.lineBottom + pageOffset + textPage.height - ChapterProvider.paddingTop,
selectedPaint
)
}
}
}
} else {
textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint
} else {
ChapterProvider.contentPaint
}
textPaint.color = if (textLine.isReadAloud) {
context.accentColor
} else {
ReadBookConfig.durConfig.textColor()
}
textLine.textChars.forEach {
canvas.drawText(
it.charData,
it.start,
textLine.lineBase + pageOffset,
textPaint
)
if (it.selected) {
canvas.drawRect(
it.start,
textLine.lineTop + pageOffset,
it.end,
textLine.lineBottom + pageOffset,
selectedPaint
)
}
}
}
}
} }
fun onScroll(mOffset: Float) { fun onScroll(mOffset: Float) {
var offset = mOffset if (mOffset == 0f) return
var offset = -mOffset
if (offset > maxScrollOffset) { if (offset > maxScrollOffset) {
offset = maxScrollOffset offset = maxScrollOffset
} else if (offset < -maxScrollOffset) { } else if (offset < -maxScrollOffset) {
@ -104,6 +199,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
pageOffset += offset pageOffset += offset
invalidate()
} }
fun resetPageOffset() { fun resetPageOffset() {

@ -10,11 +10,11 @@ abstract class PageFactory<DATA>(protected val dataSource: DataSource) {
abstract fun moveToPrevious(): Boolean abstract fun moveToPrevious(): Boolean
abstract fun nextPage(): DATA? abstract val nextPage: DATA?
abstract fun previousPage(): DATA? abstract val prevPage: DATA?
abstract fun currentPage(): DATA? abstract val currentPage: DATA?
abstract fun hasNext(): Boolean abstract fun hasNext(): Boolean

@ -100,15 +100,15 @@ class PageView(context: Context, attrs: AttributeSet) :
fun upContent(position: Int = 0) { fun upContent(position: Int = 0) {
if (ReadBookConfig.isScroll) { if (ReadBookConfig.isScroll) {
curPage.setContent(pageFactory.currentPage()) curPage.setContent(pageFactory.currentPage)
} else { } else {
when (position) { when (position) {
-1 -> prevPage.setContent(pageFactory.previousPage()) -1 -> prevPage.setContent(pageFactory.prevPage)
1 -> nextPage.setContent(pageFactory.nextPage()) 1 -> nextPage.setContent(pageFactory.nextPage)
else -> { else -> {
curPage.setContent(pageFactory.currentPage()) curPage.setContent(pageFactory.currentPage)
nextPage.setContent(pageFactory.nextPage()) nextPage.setContent(pageFactory.nextPage)
prevPage.setContent(pageFactory.previousPage()) prevPage.setContent(pageFactory.prevPage)
} }
} }
} }

@ -52,11 +52,13 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
false false
} }
override fun currentPage(): TextPage? = with(dataSource) { override val currentPage: TextPage?
get() = with(dataSource) {
return getCurrentChapter()?.page(pageIndex) return getCurrentChapter()?.page(pageIndex)
} }
override fun nextPage(): TextPage? = with(dataSource) { override val nextPage: TextPage?
get() = with(dataSource) {
getCurrentChapter()?.let { getCurrentChapter()?.let {
if (pageIndex < it.pageSize() - 1) { if (pageIndex < it.pageSize() - 1) {
return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan() return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan()
@ -65,7 +67,8 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
return getNextChapter()?.page(0)?.removePageAloudSpan() return getNextChapter()?.page(0)?.removePageAloudSpan()
} }
override fun previousPage(): TextPage? = with(dataSource) { override val prevPage: TextPage?
get() = with(dataSource) {
if (pageIndex > 0) { if (pageIndex > 0) {
return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan() return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan()
} }

Loading…
Cancel
Save