diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 16b198626..7b68d844d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -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(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(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 diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index fe56a18be..9d0959177 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -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() } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt index 74136ba5c..7618ddc51 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt @@ -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 } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/content/BaseContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/content/BaseContentTextView.kt new file mode 100644 index 000000000..0f0693db3 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/page/content/BaseContentTextView.kt @@ -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, + 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 + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/content/ContentTextView.kt similarity index 69% rename from app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt rename to app/src/main/java/io/legado/app/ui/book/read/page/content/ContentTextView.kt index 7d2262824..be3057a48 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/content/ContentTextView.kt @@ -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, - 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 - } } diff --git a/app/src/main/res/layout/view_book_page.xml b/app/src/main/res/layout/view_book_page.xml index 81c9e09d4..325e8a75c 100644 --- a/app/src/main/res/layout/view_book_page.xml +++ b/app/src/main/res/layout/view_book_page.xml @@ -32,7 +32,7 @@ android:background="@color/divider" android:visibility="invisible" /> -