0

I'm using fresco image loader library in my app. But since fresco drawee view doesn't support wrap content or adjust view. I just want to get image aspect ratio and set that ratio to drawee view programmatically. How can I do that?

uzaysan
  • 583
  • 1
  • 4
  • 18

1 Answers1

0

Here the code to get aspect ratio using Fresco. You must use BaseControllerListener to get the width and height of image and then calculate the aspect ratio = width / height

    val listener = object : BaseControllerListener<ImageInfo>() {

            override fun onFinalImageSet(id: String?, @Nullable imageInfo: ImageInfo?, @Nullable animatable: Animatable?) {
                //Action on final image load
                if (imageInfo != null) {
                    listHolder.mPic.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT
                    listHolder.mPic.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT
                    listHolder.mPic.setAspectRatio(imageInfo.width.toFloat() / imageInfo.height)
                }
            }

            override fun onFailure(id: String?, throwable: Throwable?) {
                //Action on failure
            }

        }
        val request = ImageRequest.fromUri(urlImg)
        val controller: DraweeController = Fresco.newDraweeControllerBuilder()
            .setUri(urlImg)
            .setControllerListener(listener)
            .build()
        listHolder.mPic.controller = controller