简体   繁体   English

使用Fresco以正确的纵横比从文件加载图像

[英]Load image from file in correct aspect ratio using Fresco

I have to load an image into SimpleDraweeView from either a URL or a filename/Uri. 我必须从URL或文件名/ Uri将图像加载到SimpleDraweeView

This is my xml layout: 这是我的xml布局:

<com.facebook.drawee.view.SimpleDraweeView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    fresco:actualImageScaleType="centerCrop"
                    fresco:placeholderImage="@color/wait_color"
                    fresco:placeholderImageScaleType="centerCrop"
                    fresco:roundTopLeft="true"
                    fresco:roundTopRight="true"
                    fresco:roundBottomLeft="false"
                    fresco:roundBottomRight="false"
                    fresco:roundedCornerRadius="3dp" />

When I load an image from URLs such as Facebook or Instagram, I get the width and height of the image and based on that, I calculate the aspect ratio and set it to the SimpleDraweeView like this: 当我从诸如Facebook或Instagram之类的URL加载图像时,我得到图像的widthheight ,并以此为基础计算宽高比,并将其设置为SimpleDraweeView如下所示:

imageUrl = intent.getExtras().getString(KEY_IMAGE_URL);

                int width = intent.getExtras().getInt(KEY_WIDTH);
                int height = intent.getExtras().getInt(KEY_HEIGHT);

                float aspectRatio = (float) width / height;

                image.setAspectRatio(aspectRatio);

                Uri uri = Uri.parse(imageUrl);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setProgressiveRenderingEnabled(true)
                        .setAutoRotateEnabled(true)
                        .build();
                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(image.getController())
                        .build();
                image.setController(controller);

Now, I need to be able to load the image into SimpleDraweeView using filename or Uri with correct aspect ratio, how do I do that? 现在,我需要能够以正确的宽高比使用filenameUri将图像加载到SimpleDraweeView ,该怎么办?

In general, we do not encourage people to use this approach since Drawee may show more than 1 thing and there is no real intrinsic size. 通常,我们不鼓励人们使用此方法,因为Drawee可能会显示1件以上的东西,并且没有实际的内在尺寸。 Read more about the reasons why wrap_content is not supported here . 在此处详细了解不支持wrap_content的原因 This page also has a link in the last paragraph how a controller listener can be used for this . 该页面的最后一段中还有一个链接,说明如何将控制器侦听器用于此目的 However, it might be better to consider a different approach due to all the issues outlined in the Fresco documentation. 但是,由于Fresco文档中列出的所有问题,最好考虑采用其他方法。 For example, a fixed size and an appropriate scale type could work better. 例如,固定大小和适当的比例类型可以更好地工作。

Try this: 尝试这个:
set adjustViewBounds = true 设置adjustViewBounds = true

Solved it. 解决了。 Just got the width and height of the image and calculated the aspect ratio. 刚得到图像的宽度和高度并计算出纵横比。

filename = intent.getExtras().getString(KEY_FILE);
            if (!TextUtils.isEmpty(filename)) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filename, options);
                int height = options.outHeight;
                int width = options.outWidth;

                f = new File(filename);
                Uri uri = Uri.fromFile(f);

                float aspectRatio = (float) width / height;

                image.setAspectRatio(aspectRatio);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setProgressiveRenderingEnabled(true)
                        .setAutoRotateEnabled(true)
                        .build();
                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(image.getController())
                        .build();
                image.setController(controller);
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM