Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

graphics - How to scale path and photo together in android

I'm working for draw on photo android app. I'm trying to add zoom photo and drawn path together.

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        initialize()
        invalidate()
    }
    
    private fun initialize() {
        viewWidth = measuredWidth.toFloat()
        viewHeight = measuredHeight.toFloat()
        viewRect.set(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat())
        initializeBitmapMatrix()
    }
    
    private fun initializeBitmapMatrix() {
        val scale = min(viewWidth / bitmapRect.width(), viewHeight / bitmapRect.height())
        bitmapMatrix.setScale(scale, scale)
    
        val translateX = (viewWidth - bitmapRect.width() * scale) / 2f
        val translateY = (viewHeight - bitmapRect.height() * scale) / 2f
        bitmapMatrix.postTranslate(translateX, translateY)
    }
    
    fun setBitmap(bitmap: Bitmap?) {
        this.bitmap = bitmap
    
        bitmapRect.set(
                0f,
                0f,
                this.bitmap?.width?.toFloat() ?: 0f,
                this.bitmap?.height?.toFloat() ?: 0f
        )
       
        initialize()
        invalidate()
    }

My gesture listener:

override fun onScale(scaleFactor: Float, focusX: Float, focusY: Float) {

        bitmapMatrix.preScale(
                scaleFactor,
                scaleFactor,
                bitmapRect.centerX(),
                bitmapRect.centerY()
        )


        val pathComputeBounds = RectF()
        path.computeBounds(pathComputeBounds, true)

        paint.strokeWidth = paint.strokeWidth * scaleFactor
        pathMatrix.setScale(scaleFactor, scaleFactor,
                pathComputeBounds.centerX(), pathComputeBounds.centerY()
        )
        path.transform(pathMatrix)

        invalidate()
    }

When I zoom photo and path It doesn't scale correctly. You see before zoom/after zoom on photo below. How can I fix this issue?

diff before zoom and after

question from:https://stackoverflow.com/questions/65919029/how-to-scale-path-and-photo-together-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...