Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Sep 22, 2023
1 parent 49e5063 commit 0b82858
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,11 @@ class CoverPageDelegate(readView: ReadView) : HorizontalPageDelegate(readView) {
override fun setBitmap() {
when (mDirection) {
PageDirection.PREV -> {
prevBitmap?.recycle()
prevBitmap = prevPage.screenshot()
prevBitmap = prevPage.screenshot(prevBitmap, canvas)
}
PageDirection.NEXT -> {
nextBitmap?.recycle()
nextBitmap = nextPage.screenshot()
curBitmap?.recycle()
curBitmap = curPage.screenshot()
nextBitmap = nextPage.screenshot(nextBitmap, canvas)
curBitmap = curPage.screenshot(curBitmap, canvas)
}
else -> Unit
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.legado.app.ui.book.read.page.delegate

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.MotionEvent
import io.legado.app.ui.book.read.page.ReadView
import io.legado.app.ui.book.read.page.entities.PageDirection
Expand All @@ -11,6 +12,7 @@ abstract class HorizontalPageDelegate(readView: ReadView) : PageDelegate(readVie
protected var curBitmap: Bitmap? = null
protected var prevBitmap: Bitmap? = null
protected var nextBitmap: Bitmap? = null
protected var canvas: Canvas = Canvas()
private val slopSquare by lazy { readView.slopSquare * readView.slopSquare }

override fun setDirection(direction: PageDirection) {
Expand All @@ -21,17 +23,13 @@ abstract class HorizontalPageDelegate(readView: ReadView) : PageDelegate(readVie
open fun setBitmap() {
when (mDirection) {
PageDirection.PREV -> {
prevBitmap?.recycle()
prevBitmap = prevPage.screenshot()
curBitmap?.recycle()
curBitmap = curPage.screenshot()
prevBitmap = prevPage.screenshot(prevBitmap, canvas)
curBitmap = curPage.screenshot(curBitmap, canvas)
}

PageDirection.NEXT -> {
nextBitmap?.recycle()
nextBitmap = nextPage.screenshot()
curBitmap?.recycle()
curBitmap = curPage.screenshot()
nextBitmap = nextPage.screenshot(nextBitmap, canvas)
curBitmap = curPage.screenshot(curBitmap, canvas)
}

else -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class NoAnimPageDelegate(readView: ReadView) : HorizontalPageDelegate(readView)
stopScroll()
}

override fun setBitmap() {
// nothing
}

override fun onDraw(canvas: Canvas) {
// nothing
}
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/io/legado/app/utils/ViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.os.Build
import android.text.Html
import android.view.MotionEvent
Expand Down Expand Up @@ -133,12 +134,21 @@ fun View.visible(visible: Boolean) {
}
}

fun View.screenshot(): Bitmap? {
fun View.screenshot(bitmap: Bitmap? = null, canvas: Canvas? = null): Bitmap? {
return if (width > 0 && height > 0) {
val screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val c = Canvas(screenshot)
val screenshot = if (bitmap != null && bitmap.width == width && bitmap.height == height) {
bitmap.eraseColor(Color.TRANSPARENT)
bitmap
} else {
bitmap?.recycle()
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
}
val c = canvas ?: Canvas()
c.setBitmap(screenshot)
c.save()
c.translate(-scrollX.toFloat(), -scrollY.toFloat())
this.draw(c)
c.restore()
c.setBitmap(null)
screenshot.prepareToDraw()
screenshot
Expand Down

0 comments on commit 0b82858

Please sign in to comment.