Refine BitmapPainter
This commit is contained in:
1 parent
9c9b8b74cd
commit
f144ac701d
2 files changed
+17
-27
No files matched your search
@@ -3,43 +3,37 @@ package com.hippo.ehviewer.ui.reader
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Matrix
|
||||
import android.graphics.Paint
|
||||
import android.graphics.RectF
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.drawscope.DrawScope
|
||||
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
|
||||
import androidx.compose.ui.graphics.nativeCanvas
|
||||
import androidx.compose.ui.graphics.painter.Painter
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import androidx.compose.ui.unit.toOffset
|
||||
import androidx.compose.ui.unit.toSize
|
||||
import androidx.compose.ui.unit.IntRect
|
||||
import androidx.compose.ui.util.fastRoundToInt
|
||||
|
||||
class BitmapPainter(
|
||||
private val image: Bitmap,
|
||||
srcOffset: IntOffset = IntOffset.Zero,
|
||||
srcSize: IntSize = IntSize(image.width, image.height),
|
||||
) : Painter() {
|
||||
class BitmapPainter(private val bitmap: Bitmap, srcRect: IntRect) : Painter() {
|
||||
|
||||
private val offset = (-srcOffset).toOffset()
|
||||
private val srcRect = with(srcRect) {
|
||||
RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
|
||||
}
|
||||
|
||||
private val srcSize = srcSize.toSize()
|
||||
private val dstRect = RectF()
|
||||
|
||||
private val matrix = Matrix()
|
||||
|
||||
// Use the overload that takes a `Matrix` to bypass the 100 MB size limit
|
||||
override fun DrawScope.onDraw() = drawIntoCanvas {
|
||||
// Use the overload that takes a `Matrix` to bypass the 100 MB size limit
|
||||
it.nativeCanvas.drawBitmap(
|
||||
image,
|
||||
matrix.apply {
|
||||
setTranslate(offset.x, offset.y)
|
||||
postScale(size.width / srcSize.width, size.height / srcSize.height)
|
||||
},
|
||||
bitmapPaint,
|
||||
)
|
||||
dstRect.right = size.width.fastRoundToInt().toFloat()
|
||||
dstRect.bottom = size.height.fastRoundToInt().toFloat()
|
||||
matrix.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.FILL)
|
||||
it.nativeCanvas.drawBitmap(bitmap, matrix, paint)
|
||||
}
|
||||
|
||||
override val intrinsicSize get() = srcSize
|
||||
override val intrinsicSize get() = Size(srcRect.width(), srcRect.height())
|
||||
|
||||
companion object {
|
||||
private val bitmapPaint = Paint().apply {
|
||||
private val paint = Paint().apply {
|
||||
isAntiAlias = true
|
||||
isFilterBitmap = true
|
||||
isDither = true
|
||||
|
||||
@@ -117,11 +117,7 @@ fun PagerItem(
|
||||
}
|
||||
|
||||
private fun Image.toPainter() = when (val image = innerImage) {
|
||||
is BitmapImage -> BitmapPainter(
|
||||
image = image.bitmap,
|
||||
srcOffset = rect.topLeft,
|
||||
srcSize = rect.size,
|
||||
)
|
||||
is BitmapImage -> BitmapPainter(image.bitmap, rect)
|
||||
is DrawableImage -> DrawablePainter(image.drawable)
|
||||
else -> unreachable()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user