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?
Asked
Active
Viewed 550 times
0
-
[This](https://stackoverflow.com/questions/34874463/simpledraweeview-not-resizing-after-scaling-image-in-fresco) might be helpful. – itsmysterybox Oct 03 '18 at 13:36
-
I couldnt use the code editor always give me error? @itsmysterybox – uzaysan Oct 03 '18 at 20:11
-
What kind of error? – itsmysterybox Oct 03 '18 at 20:19
1 Answers
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

Le Trung Hieu
- 154
- 5