Cannot scale PhotoView with specific position in image resource

54 views Asked by At

I have a photoView with fixed height and width below.

enter image description here

The image demomap.jpg is used to load into photoView. By default, the image will be center of the imageview like this:

enter image description here

I follow the same approach aslansari and a post here.

The orgin size of the image is: 638x612. When running, the photoview bitmap will be auto expand to 1675x1607. So I add a scaleRate variable to map from real position to bitmap position. The problem is, the center of the photoview doesn't coincides with the position (442,300) in real image ( or () in bitmap). The correct position is offset from the photoview's center position by about 100 px in this case.

 val photoView = binding.photoView;
        photoView.scale = 1f
        photoView.setImageResource(R.drawable.demomap);

        val bitmap = photoView.drawable.toBitmap()

        val options = BitmapFactory.Options()
        options.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT
        val bd = BitmapFactory.decodeResource(
            requireActivity().resources,
            R.drawable.demomap,
            options
        )
        val imageRealWidth = bd.width.toFloat()

        val scaleRate = bitmap.width / imageRealWidth;

        val demoBut = binding.button2

        Log.d("bitmap-size", "${bitmap.width.toFloat()}x${bitmap.height.toFloat()}") //1675.0x1607.0

        demoBut.setOnClickListener {
            Log.d("scaleRate", scaleRate.toString()) //2.625392

            val x = 442f * scaleRate
            val y = 300f * scaleRate

            Log.d("focal x - y", "${x}-${y}") //1160.4232-787.6176

            val focalX: Float = x * photoView.right / bitmap.width
            val focalY: Float = y * photoView.bottom / bitmap.height

            val targetScale = 3f
            photoView.attacher.setScale(targetScale, focalX, focalY, false)
        }

I wonder how to fix this problem, or some thing wrong with this approach?

0

There are 0 answers