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
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…