壁画:如何在下载前获取图像长宽比?

时间:2018-10-03 12:45:25

标签: android fresco

我在我的应用程序中使用了壁画图像加载器库。但是,由于壁画窗口视图不支持包装内容或调整视图。我只想获取图像长宽比,并以编程方式将该比例设置为“糖衣橱视图”。我该怎么办?

1 个答案:

答案 0 :(得分:0)

这里是使用Fresco获取长宽比的代码。必须使用BaseControllerListener来获取图像的宽度和高度,然后计算纵横比= 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