feat: 优化代码

pull/117/head
kunfei 5 years ago
parent b1ecf66769
commit 8079f7a589
  1. 9
      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. 8
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  4. 113
      app/src/main/java/io/legado/app/ui/book/read/page/content/BaseContentTextView.kt
  5. 111
      app/src/main/java/io/legado/app/ui/book/read/page/content/ContentTextView.kt
  6. 2
      app/src/main/res/layout/view_book_page.xml
  7. 2
      build.gradle
  8. 4
      gradle/wrapper/gradle-wrapper.properties

@ -8,7 +8,6 @@ import android.os.Bundle
import android.os.Handler
import android.view.*
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.appcompat.view.menu.MenuItemImpl
import androidx.core.view.get
import androidx.core.view.isVisible
import androidx.core.view.size
@ -35,9 +34,9 @@ import io.legado.app.ui.book.read.config.*
import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.BG_COLOR
import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.ContentTextView
import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.content.BaseContentTextView
import io.legado.app.ui.book.read.page.delegate.PageDelegate
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.changesource.ChangeSourceDialog
@ -59,7 +58,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
View.OnTouchListener,
PageView.CallBack,
TextActionMenu.CallBack,
ContentTextView.CallBack,
BaseContentTextView.CallBack,
ReadMenu.CallBack,
ReadAloudDialog.CallBack,
ChangeSourceDialog.CallBack,
@ -403,8 +402,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
/**
* 文本选择菜单操作
*/
override fun onMenuItemSelected(item: MenuItemImpl): Boolean {
when (item.itemId) {
override fun onMenuItemSelected(itemId: Int): Boolean {
when (itemId) {
R.id.menu_replace -> {
ReplaceEditDialog.show(supportFragmentManager, pattern = selectedText)
return true

@ -27,7 +27,7 @@ import kotlinx.android.synthetic.main.popup_action_menu.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast
@SuppressLint("RestrictedApi")
class TextActionMenu(private val context: Context, private val callBack: CallBack) :
PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
@ -70,7 +70,7 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
override fun registerListener(holder: ItemViewHolder) {
holder.itemView.onClick {
getItem(holder.layoutPosition)?.let {
if (!callBack.onMenuItemSelected(it)) {
if (!callBack.onMenuItemSelected(it.itemId)) {
onMenuItemSelected(it)
}
}
@ -150,7 +150,7 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
interface CallBack {
val selectedText: String
fun onMenuItemSelected(item: MenuItemImpl): Boolean
fun onMenuItemSelected(itemId: Int): Boolean
fun onMenuActionFinally()
}

@ -23,10 +23,12 @@ import io.legado.app.utils.removePref
object ChapterProvider {
var viewWidth = 0
var viewHeight = 0
var visibleWidth = 0
var visibleHeight = 0
var paddingLeft = 0
var paddingTop = 0
var visibleWidth = 0
var visibleHeight = 0
var visibleRight = 0
var visibleBottom = 0
private var lineSpacingExtra = 0f
private var paragraphSpacing = 0
var typeface: Typeface = Typeface.SANS_SERIF
@ -342,6 +344,8 @@ object ChapterProvider {
paddingTop = ReadBookConfig.paddingTop.dp
visibleWidth = viewWidth - paddingLeft - ReadBookConfig.paddingRight.dp
visibleHeight = viewHeight - paddingTop - ReadBookConfig.paddingBottom.dp
visibleRight = paddingLeft + visibleWidth
visibleBottom = paddingTop + visibleHeight
}
}

@ -0,0 +1,113 @@
package io.legado.app.ui.book.read.page.content
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getPrefBoolean
abstract class BaseContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var upView: ((TextPage) -> Unit)? = null
protected val selectedPaint by lazy {
Paint().apply {
color = context.getCompatColor(R.color.btn_bg_press_2)
style = Paint.Style.FILL
}
}
protected var callBack: CallBack
protected var selectLineStart = 0
protected var selectCharStart = 0
protected var selectLineEnd = 0
protected var selectCharEnd = 0
protected var textPage: TextPage = TextPage()
//滚动参数
protected val pageFactory: TextPageFactory get() = callBack.pageFactory
protected val maxScrollOffset = 100f
protected var pageOffset = 0f
protected var linePos = 0
init {
callBack = activity as CallBack
contentDescription = textPage.text
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
ChapterProvider.viewWidth = w
ChapterProvider.viewHeight = h
ChapterProvider.upSize()
textPage.format()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.clipRect(
ChapterProvider.paddingLeft,
ChapterProvider.paddingTop,
ChapterProvider.visibleRight,
ChapterProvider.visibleBottom
)
if (ReadBookConfig.isScroll) {
drawScrollPage(canvas)
} else {
drawHorizontalPage(canvas)
}
}
abstract fun drawScrollPage(canvas: Canvas)
abstract fun drawHorizontalPage(canvas: Canvas)
protected fun drawChars(
canvas: Canvas,
textChars: List<TextChar>,
lineTop: Float,
lineBase: Float,
lineBottom: Float,
isTitle: Boolean,
isReadAloud: Boolean
) {
val textPaint = if (isTitle) ChapterProvider.titlePaint else ChapterProvider.contentPaint
textPaint.color =
if (isReadAloud) context.accentColor else ReadBookConfig.durConfig.textColor()
textChars.forEach {
canvas.drawText(it.charData, it.start, lineBase, textPaint)
if (it.selected) {
canvas.drawRect(it.start, lineTop, it.end, lineBottom, selectedPaint)
}
}
}
protected fun upSelectedStart(x: Float, y: Float) {
callBack.upSelectedStart(x, y + callBack.headerHeight)
}
protected fun upSelectedEnd(x: Float, y: Float) {
callBack.upSelectedEnd(x, y + callBack.headerHeight)
}
interface CallBack {
fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect()
val headerHeight: Int
val pageFactory: TextPageFactory
}
}

@ -1,46 +1,14 @@
package io.legado.app.ui.book.read.page
package io.legado.app.ui.book.read.page.content
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.getPrefBoolean
class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var upView: ((TextPage) -> Unit)? = null
private val selectedPaint by lazy {
Paint().apply {
color = context.getCompatColor(R.color.btn_bg_press_2)
style = Paint.Style.FILL
}
}
private var callBack: CallBack
private var selectLineStart = 0
private var selectCharStart = 0
private var selectLineEnd = 0
private var selectCharEnd = 0
private var textPage: TextPage = TextPage()
//滚动参数
private val pageFactory: TextPageFactory get() = callBack.pageFactory
private val maxScrollOffset = 100f
private var pageOffset = 0f
private var linePos = 0
init {
callBack = activity as CallBack
contentDescription = textPage.text
}
class ContentTextView(context: Context, attrs: AttributeSet?) :
BaseContentTextView(context, attrs) {
fun setContent(textPage: TextPage) {
this.textPage = textPage
@ -48,24 +16,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
invalidate()
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
ChapterProvider.viewWidth = w
ChapterProvider.viewHeight = h
ChapterProvider.upSize()
textPage.format()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (ReadBookConfig.isScroll) {
drawScrollPage(canvas)
} else {
drawHorizontalPage(canvas)
}
}
private fun drawHorizontalPage(canvas: Canvas) {
override fun drawHorizontalPage(canvas: Canvas) {
textPage.textLines.forEach { textLine ->
drawChars(
canvas,
@ -79,7 +30,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
}
private fun drawScrollPage(canvas: Canvas) {
override fun drawScrollPage(canvas: Canvas) {
val mPageOffset = pageOffset
textPage.textLines.forEach { textLine ->
val lineTop = textLine.lineTop + mPageOffset
@ -110,41 +61,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
textLine.isReadAloud
)
}
pageFactory.prevPage?.textLines?.forEach { textLine ->
val yPy = mPageOffset + ChapterProvider.paddingTop
val lineTop = -textLine.lineTop + yPy
val lineBase = -textLine.lineBase + yPy
val lineBottom = -textLine.lineBottom + yPy
drawChars(
canvas,
textLine.textChars,
lineTop,
lineBase,
lineBottom,
textLine.isTitle,
textLine.isReadAloud
)
}
}
private fun drawChars(
canvas: Canvas,
textChars: List<TextChar>,
lineTop: Float,
lineBase: Float,
lineBottom: Float,
isTitle: Boolean,
isReadAloud: Boolean
) {
val textPaint = if (isTitle) ChapterProvider.titlePaint else ChapterProvider.contentPaint
textPaint.color =
if (isReadAloud) context.accentColor else ReadBookConfig.durConfig.textColor()
textChars.forEach {
canvas.drawText(it.charData, it.start, lineBase, textPaint)
if (it.selected) {
canvas.drawRect(it.start, lineTop, it.end, lineBottom, selectedPaint)
}
}
}
fun onScroll(mOffset: Float) {
@ -271,14 +187,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
invalidate()
}
private fun upSelectedStart(x: Float, y: Float) {
callBack.upSelectedStart(x, y + callBack.headerHeight)
}
private fun upSelectedEnd(x: Float, y: Float) {
callBack.upSelectedEnd(x, y + callBack.headerHeight)
}
fun cancelSelect() {
textPage.textLines.forEach { textLine ->
textLine.textChars.forEach {
@ -317,11 +225,4 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
return stringBuilder.toString()
}
interface CallBack {
fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect()
val headerHeight: Int
val pageFactory: TextPageFactory
}
}

@ -32,7 +32,7 @@
android:background="@color/divider"
android:visibility="invisible" />
<io.legado.app.ui.book.read.page.ContentTextView
<io.legado.app.ui.book.read.page.content.ContentTextView
android:id="@+id/content_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"

@ -9,7 +9,7 @@ buildscript {
maven { url 'https://plugins.gradle.org/m2/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'de.timfreiheit.resourceplaceholders:placeholders:0.3'
classpath 'com.google.gms:google-services:4.3.3'

@ -1,6 +1,6 @@
#Wed Aug 21 20:30:49 CST 2019
#Tue Feb 25 08:10:32 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

Loading…
Cancel
Save