159 lines
5.1 KiB
Kotlin
159 lines
5.1 KiB
Kotlin
|
|
package com.shitoucuo.postpad
|
|||
|
|
|
|||
|
|
import android.content.Context
|
|||
|
|
import android.graphics.Bitmap
|
|||
|
|
import android.graphics.Canvas
|
|||
|
|
import android.graphics.Matrix
|
|||
|
|
import android.graphics.RectF
|
|||
|
|
import android.view.GestureDetector
|
|||
|
|
import android.view.MotionEvent
|
|||
|
|
import android.view.ScaleGestureDetector
|
|||
|
|
import android.view.View
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 图片缩放视图:双指缩放、拖动、双击放大/还原、单击关闭。
|
|||
|
|
* 用于阅读页 FlexPhoto 图组点击放大查看。
|
|||
|
|
*/
|
|||
|
|
class ZoomableImageView(context: Context, private val onTapClose: () -> Unit) : View(context) {
|
|||
|
|
|
|||
|
|
private var bitmap: Bitmap? = null
|
|||
|
|
private val drawMatrix = Matrix()
|
|||
|
|
private val saved = Matrix()
|
|||
|
|
private val displayRect = RectF()
|
|||
|
|
private var minScale = 0.5f
|
|||
|
|
private val maxScale = 6f
|
|||
|
|
|
|||
|
|
private val scaleDetector = ScaleGestureDetector(
|
|||
|
|
context,
|
|||
|
|
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
|||
|
|
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean {
|
|||
|
|
saved.set(drawMatrix)
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
|||
|
|
drawMatrix.set(saved)
|
|||
|
|
drawMatrix.postScale(
|
|||
|
|
detector.scaleFactor, detector.scaleFactor,
|
|||
|
|
detector.focusX, detector.focusY,
|
|||
|
|
)
|
|||
|
|
clamp()
|
|||
|
|
invalidate()
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
private val gestureDetector = GestureDetector(
|
|||
|
|
context,
|
|||
|
|
object : GestureDetector.SimpleOnGestureListener() {
|
|||
|
|
override fun onDown(e: MotionEvent): Boolean {
|
|||
|
|
saved.set(drawMatrix)
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onScroll(e1: MotionEvent?, e2: MotionEvent, dx: Float, dy: Float): Boolean {
|
|||
|
|
drawMatrix.set(saved)
|
|||
|
|
drawMatrix.postTranslate(-dx, -dy)
|
|||
|
|
clamp()
|
|||
|
|
invalidate()
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onDoubleTap(e: MotionEvent): Boolean {
|
|||
|
|
val current = scale()
|
|||
|
|
if (current > minScale * 1.2f) {
|
|||
|
|
resetFit()
|
|||
|
|
} else {
|
|||
|
|
drawMatrix.postScale(2.5f / current, 2.5f / current, e.x, e.y)
|
|||
|
|
clamp()
|
|||
|
|
}
|
|||
|
|
invalidate()
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
|
|||
|
|
onTapClose()
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
fun setBitmap(bmp: Bitmap) {
|
|||
|
|
bitmap = bmp
|
|||
|
|
resetFit()
|
|||
|
|
invalidate()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onDraw(canvas: Canvas) {
|
|||
|
|
super.onDraw(canvas)
|
|||
|
|
val bmp = bitmap ?: return
|
|||
|
|
canvas.drawBitmap(bmp, drawMatrix, null)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|||
|
|
scaleDetector.onTouchEvent(event)
|
|||
|
|
gestureDetector.onTouchEvent(event)
|
|||
|
|
if (event.actionMasked == MotionEvent.ACTION_UP ||
|
|||
|
|
event.actionMasked == MotionEvent.ACTION_CANCEL
|
|||
|
|
) {
|
|||
|
|
saved.set(drawMatrix)
|
|||
|
|
}
|
|||
|
|
return true
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
|||
|
|
super.onSizeChanged(w, h, oldw, oldh)
|
|||
|
|
resetFit()
|
|||
|
|
invalidate()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private fun scale(): Float {
|
|||
|
|
val values = FloatArray(9)
|
|||
|
|
drawMatrix.getValues(values)
|
|||
|
|
return values[Matrix.MSCALE_X]
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 初始按视图大小完整显示 */
|
|||
|
|
private fun resetFit() {
|
|||
|
|
val bmp = bitmap ?: return
|
|||
|
|
val w = width.toFloat()
|
|||
|
|
val h = height.toFloat()
|
|||
|
|
if (w <= 0 || h <= 0) return
|
|||
|
|
drawMatrix.reset()
|
|||
|
|
val scale = minOf(w / bmp.width, h / bmp.height)
|
|||
|
|
drawMatrix.postScale(scale, scale, w / 2, h / 2)
|
|||
|
|
drawMatrix.postTranslate(w / 2 - bmp.width * scale / 2, h / 2 - bmp.height * scale / 2)
|
|||
|
|
minScale = scale * 0.8f
|
|||
|
|
clamp()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 缩放限制在 [minScale, maxScale],位移保证图片不会完全拖出屏幕 */
|
|||
|
|
private fun clamp() {
|
|||
|
|
val bmp = bitmap ?: return
|
|||
|
|
val values = FloatArray(9)
|
|||
|
|
drawMatrix.getValues(values)
|
|||
|
|
val s = values[Matrix.MSCALE_X].coerceIn(minScale, maxScale)
|
|||
|
|
if (values[Matrix.MSCALE_X] != s) {
|
|||
|
|
drawMatrix.setScale(s, s, width / 2f, height / 2f)
|
|||
|
|
}
|
|||
|
|
displayRect.set(0f, 0f, bmp.width.toFloat(), bmp.height.toFloat())
|
|||
|
|
drawMatrix.mapRect(displayRect)
|
|||
|
|
var dx = 0f
|
|||
|
|
var dy = 0f
|
|||
|
|
if (displayRect.width() <= width) {
|
|||
|
|
dx = width / 2f - displayRect.centerX()
|
|||
|
|
} else {
|
|||
|
|
if (displayRect.left > 0) dx = -displayRect.left
|
|||
|
|
if (displayRect.right < width) dx = width - displayRect.right
|
|||
|
|
}
|
|||
|
|
if (displayRect.height() <= height) {
|
|||
|
|
dy = height / 2f - displayRect.centerY()
|
|||
|
|
} else {
|
|||
|
|
if (displayRect.top > 0) dy = -displayRect.top
|
|||
|
|
if (displayRect.bottom < height) dy = height - displayRect.bottom
|
|||
|
|
}
|
|||
|
|
if (dx != 0f || dy != 0f) drawMatrix.postTranslate(dx, dy)
|
|||
|
|
}
|
|||
|
|
}
|