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
321 views
in Technique[技术] by (71.8m points)

android - How to get Width and Height of an dynamically added ImageView having adjustViewBounds "true" and animateLayoutChanges "true" in Kotlin?

I know there are similar questions already asked before and I have tried all of them as below:

private fun addImageToBoardWindow(key: String) {
        if (childViews.containsKey(key)) return
        var viewWidth = 0
        var viewHeight = 0
        var keyUriString = ""
        board_window.addView(
            LayoutInflater.from(this)
                .inflate(R.layout.board_image_view, board_window, false)?.also { pview ->
                    childViews[key] = pview
                    pview.findViewById<ImageView>(R.id.board_image).apply {

                        val vto: ViewTreeObserver = viewTreeObserver
                        vto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
                            override fun onPreDraw(): Boolean {
                                viewTreeObserver.removeOnPreDrawListener(this)
                                viewHeight = measuredHeight
                                viewWidth = measuredWidth
                                Log.i(" :BoardActivity: ", "onPreDraw: width: $viewWidth and height: $viewHeight")
                                saveScales(
                                    keyUriString,
                                    Pair(viewWidth.toFloat(), viewHeight.toFloat())
                                )
                                return true
                            }
                        })

                        viewTreeObserver
                            .addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
                                override fun onGlobalLayout() {
                                    viewTreeObserver.removeOnGlobalLayoutListener(this)
                                    viewHeight = measuredHeight
                                    viewWidth = measuredWidth
                                    Log.i(" :BoardActivity: ", "onGlobalLayout: width: $viewWidth and height: $viewHeight")
                                    saveScales(
                                        keyUriString,
                                        Pair(viewWidth.toFloat(), viewHeight.toFloat())
                                    )
                                }
                            })

                        post(Runnable {
                            viewHeight = measuredHeight
                            viewWidth = measuredWidth
                            Log.i(" :BoardActivity: ", "post: width: $viewWidth and height: $viewHeight")
                            saveScales(
                                keyUriString,
                                Pair(viewWidth.toFloat(), viewHeight.toFloat())
                            )
                        })

                        masterData[key]?.let { sketch ->
                            keyUriString = sketch.data
                            Glide.with(this@BoardActivity).load(sketch.data).into(this)
                            sketch.matrix?.let {
                                // comment by srdpatel: 1/11/2021 Find image path and store in bitmaps along with scale
                                saveScales(
                                    sketch.data,
                                    Pair(viewWidth.toFloat(), viewHeight.toFloat())
                                )
                                updateViewMatrix(this, sketch.data, it)
                            }
                        }
//                        setOnClickListener {
//                            if (whiteBoard.drawingMode == DrawingModes.ERASER) {
//                                board_window.removeView(view)
//                            }
//                        }
                        setOnTouchListener { view, event ->
                            if (isUserBoard()) {
                                if (whiteBoard.drawingMode == DrawingModes.ERASER) {
                                    masterData.remove(key)
                                    removePointsInServer(key)
                                    board_window.removeView(pview)
                                } else
                                    viewTransformation(view, key, event)
                            }
                            true
                        }
                        val location = IntArray(2)
                        getLocationInWindow(location)
                        Log.i("board_image", "${location[0]} : ${location[1]}")
                    }
                }
        )
    }

Unfortunately, all the methods to get the width and height of a dynamically added imageView (R.id.board_image) are returning 0!!! We are interested in the width and height of the imageView and not the image itself. Once again, the imageView XML part (R.id.board_image) has been set to adjustViewBounds true and animateLayoutChanges also true. The imageView has width and height set as wrap_content.


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

1 Reply

0 votes
by (71.8m points)
等待大神答复

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

...