国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin

這篇具有很好參考價(jià)值的文章主要介紹了Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin

?文章來源地址http://www.zghlxwxcb.cn/news/detail-818712.html

在?Android雙擊圖片放大移動(dòng)圖中雙擊點(diǎn)到ImageView區(qū)域中心,Kotlin-CSDN博客 基礎(chǔ)上,這次使用ScaleGestureDetector檢測(cè)兩根手指的縮放動(dòng)作,記錄兩根手指的中心點(diǎn)位置,根據(jù)ScaleGestureDetector檢測(cè)到的縮放因子(系數(shù)),放大原圖,并移動(dòng)放大前兩根手指中心點(diǎn)在原圖的位置移動(dòng)到放大后區(qū)域。

?

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 testIV: ImageView? = null

    //放大系數(shù)。
    private var mScaleFactor = 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 > 10) {
                    mScaleFactor = detector.scaleFactor
                }
            }

            override fun onScale(detector: ScaleGestureDetector): Boolean {
                //更新兩個(gè)手指縮放的中心點(diǎn)。
                mCenterX = detector.focusX
                mCenterY = detector.focusY

                return super.onScale(detector)
            }
        })
    }

    fun setTestImageView(iv: ImageView?) {
        testIV = iv
    }

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

    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.actionMasked) {
            MotionEvent.ACTION_MOVE -> {
                mScaleGestureDetector?.onTouchEvent(event)
                mCanDrawCircle = true
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                mCanDrawScaleBmp = true //兩個(gè)手指松開了,可以繪制放大的圖。
                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) {
        Thread.sleep(1500)

        if (mScaleBmp == null) {
            //創(chuàng)建一次,避免重復(fù)創(chuàng)建,提高速度。
            mScaleBmp = Bitmap.createScaledBitmap(
                mSrcBmp!!,
                (this.width * mScaleFactor + 1).toInt(), //注意這里的精度損失,會(huì)造成坐標(biāo)偏移.
                (this.height * mScaleFactor + 1).toInt(),//注意這里的精度損失,會(huì)造成坐標(biāo)偏移.
                true
            )
        }

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

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

        //中心圓圈
        canvas.drawCircle(cx, cy, 40f, mCirclePaint)
    }
}

?

?

兩根手指在原圖上縮放,紅色小圓圈實(shí)時(shí)處于兩根手指的中心點(diǎn):

Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin,kotlin,Android?,android,kotlin

?

Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin,kotlin,Android?,android,kotlin

?

?

?

當(dāng)兩根手指離開屏幕后,然后放大原圖,并把原先手指中心點(diǎn)“點(diǎn)中”的原圖位置,移動(dòng)到屏幕中心:

Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin,kotlin,Android?,android,kotlin

?

?

Android雙擊圖片放大移動(dòng)圖中雙擊點(diǎn)到ImageView區(qū)域中心,Kotlin-CSDN博客文章瀏覽閱讀663次,點(diǎn)贊14次,收藏17次。需要注意的,因?yàn)樵趚ml布局里面特別設(shè)置了ImageView的高度為wrap_content,手指在屏幕觸點(diǎn)的位置是放大鏡里面放大圖片后準(zhǔn)確圓心位置,但是,如果ImageView設(shè)置成match_parent,則因?yàn)镮mageView里面的Bitmap被縮放(此處Bitmap其實(shí)小于ImageView,被拉伸了),拉伸后的Bitmap水平方向坐標(biāo)與ImageView一直重合,但豎直方向,Bitmap坐標(biāo)與ImageView不一致,會(huì)造成一種現(xiàn)象,手指觸點(diǎn)放大鏡放大后,水平方向是正確的,但豎直方向有偏移量。https://blog.csdn.net/zhangphil/article/details/135630975

?

到了這里,關(guān)于Android雙指縮放ScaleGestureDetector檢測(cè)放大因子大圖移動(dòng)到雙指中心點(diǎn)ImageView區(qū)域中心,Kotlin的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包