Android ScaleGestureDetector检测双指缩放Bitmap基于Matrix动画移动到双指捏合中心点ImageView区域中心,Kotlin

Android ScaleGestureDetector检测双指缩放Bitmap基于Matrix动画移动到双指捏合中心点ImageView区域中心,Kotlin

在 Android双指缩放ScaleGestureDetector检测放大因子大图移动到双指中心点ImageView区域中心,Kotlin-CSDN博客 实现了双指拟合缩放原始图,并将原始捏合点的中心作为缩放中心,并最终呈现在界面里面以这个中心点为缩放中心的放大后的大图,但是这个过程是一瞬间的,没有动画,现在改进,实现把Bitmap基于Matrix的缩放,在缩放过程中使用渐变的动画,缩放过程逐渐放大,且最终的放大中心是两指在屏幕的捏合点中心位置。

class MyImageView : AppCompatImageView {
    private var mCenterX = 0f
    private var mCenterY = 0f

    private val mCirclePaint = Paint()

    private var mSrcBmp: Bitmap? = null
    private var mScaleBmp: Bitmap? = null

    //放大系数。
    private var mScaleFactor = 1f
    private var mGradientScaleFactor = 1f

    private var mScaleGestureDetector: ScaleGestureDetector? = null

    private var mCanDrawScaleBmp = false
    private var mCanDrawCircle = false

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {
        mSrcBmp = (drawable as BitmapDrawable).bitmap //mSrcBmp是原始图大小,没有缩放和拉伸的。

        mCirclePaint.style = Paint.Style.STROKE
        mCirclePaint.strokeWidth = 10f
        mCirclePaint.isAntiAlias = true
        mCirclePaint.color = Color.RED

        mScaleGestureDetector = ScaleGestureDetector(ctx, object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
            override fun onScaleEnd(detector: ScaleGestureDetector) {
                super.onScaleEnd(detector)
                //if (detector.currentSpan > 50 && detector.timeDelta > 20) {
                mScaleFactor = detector.scaleFactor
                Log.d("fly", "onScaleEnd scaleFactor=${detector.scaleFactor}")
                //}

                startGradientScale()
            }

            override fun onScale(detector: ScaleGestureDetector): Boolean {
                //更新两个手指缩放的中心点。
                mCenterX = detector.focusX
                mCenterY = detector.focusY

                return false
            }
        })
    }

    fun startGradientScale() {
        val step = 1500f
        val delta = (mScaleFactor - 1f) / step
        CoroutineScope(Dispatchers.IO).launch {
            delay(1000)

            mCanDrawScaleBmp = true
            for (i in 0..step.toInt()) {
                mGradientScaleFactor = 1f + i * delta

                withContext(Dispatchers.Main) {
                    updateView()
                }
            }
        }
    }

    private fun updateView() {
        this.invalidate()
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        mScaleGestureDetector?.onTouchEvent(event)

        when (event.actionMasked) {
            MotionEvent.ACTION_MOVE -> {
                mCanDrawCircle = true
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                //如果抬手时候重新置false,没有绘制,显示原始底图。
                //mCanDrawScaleBmp = false
                mCanDrawCircle = false
            }
        }

        updateView()

        return true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if (mCanDrawCircle) {
            canvas.drawCircle(mCenterX, mCenterY, 10f, mCirclePaint)
        }

        if (mCanDrawScaleBmp) {
            myDraw(canvas)
        }
    }

    private fun myDraw(canvas: Canvas) {
        mScaleBmp = Bitmap.createScaledBitmap(
            mSrcBmp!!,
            (this.width * mGradientScaleFactor + 1).toInt(), //注意这里的精度损失,会造成坐标偏移.
            (this.height * mGradientScaleFactor + 1).toInt(),//注意这里的精度损失,会造成坐标偏移.
            true
        )

        val cx = this.width / 2f
        val cy = this.height / 2f

        val matrix = Matrix()
        matrix.setScale(mGradientScaleFactor, mGradientScaleFactor)
        matrix.setTranslate(cx - mCenterX * mGradientScaleFactor, cy - mCenterY * mGradientScaleFactor)
        canvas.drawBitmap(mScaleBmp!!, matrix, null)

        //中心圆圈
        canvas.drawCircle(cx, cy, 50f, mCirclePaint)
    }
}

当两个手指在屏幕捏合时候,在屏幕上用红色小圆圈实时绘制出两个手指之间的中心点位置:

当两个手指抬离屏幕后,开始动画逐渐放大原图,用大的红色圆圈标记出ImageView的中心区域,可以看到,原来的Bitmap开始呈现逐渐放大的过程变化:

Android ScaleGestureDetector检测双指缩放Bitmap基于Matrix动画移动到双指捏合中心点ImageView区域中心,Kotlin_第1张图片

缩放过程结束后,最终定格:

遗留问题:动画在缩放过程中有轻微的顿挫感,当step数值越小时候,动画缩放的顿挫感更名下。

Android双指缩放ScaleGestureDetector检测放大因子大图移动到双指中心点ImageView区域中心,Kotlin-CSDN博客文章浏览阅读513次,点赞6次,收藏9次。需要注意的,因为在xml布局里面特别设置了ImageView的高度为wrap_content,手指在屏幕触点的位置是放大镜里面放大图片后准确圆心位置,但是,如果ImageView设置成match_parent,则因为ImageView里面的Bitmap被缩放(此处Bitmap其实小于ImageView,被拉伸了),拉伸后的Bitmap水平方向坐标与ImageView一直重合,但竖直方向,Bitmap坐标与ImageView不一致,会造成一种现象,手指触点放大镜放大后,水平方向是正确的,但竖直方向有偏移量。https://blog.csdn.net/zhangphil/article/details/135669388

你可能感兴趣的:(kotlin,Android,android,kotlin)