Search code examples
androidhuawei-mobile-services

How to adjust HMS Scan Kit CustomizedView scale in a fragment?


I want to use CustomizedView inside a fragment that is not full screen. I found that the camera viewfinder looks stretched, I've tried to adjust the width and height but nothing works. Here's the image comparison between CustomizedView in activity and fragment.

in fragment (stretched) in fragment (stretched)

in activity (normal) in activity (normal)

is there any way to fix this?


Solution

  • After inspecting the layout with the layout inspector, I found that the layout_width child view of the RemoteView is set to a specific value. TextureView width

    It can be solved with a workaround by finding the TextureView:

        private fun findTextureViewInChildren(viewGroup: ViewGroup): TextureView? {
            val childCount = viewGroup.childCount
            for (i in 0 until childCount) {
                val child: View = viewGroup.getChildAt(i)
                if (child is TextureView) {
                    return child
                } else if (child is ViewGroup) {
                    val foundInViewGroup = findTextureViewInChildren(child)
                    if (foundInViewGroup != null) {
                        return foundInViewGroup
                    }
                }
            }
            return null
        }
    

    and then setting the layout_width to match_parent:

    val surfaceView = findTextureViewInChildren(this)
    
    val layoutParams = surfaceView.layoutParams
    layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
    surfaceView.layoutParams = layoutParams