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

Android 使用Fresco以正确的纵横比从文件加载图像,android,fresco,Android,Fresco,我必须从URL或文件名/Uri将图像加载到SimpleDraweView 这是我的xml布局: <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content"

我必须从URL或文件名/Uri将图像加载到
SimpleDraweView

这是我的
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" />

现在,我需要能够使用
filename
Uri
以正确的纵横比将图像加载到
simpledraweview
中,我该怎么做

一般来说,我们不鼓励人们使用这种方法,因为付款人可能会显示不止一件东西,并且没有真实的内在大小。这个页面在最后一段中也有一个链接。
然而,由于壁画文献中所概述的所有问题,最好考虑一种不同的方法。例如,一个固定的大小和一个合适的刻度类型可以更好地工作。

试试这个:
设置
adjustViewBounds=true
解决了这个问题。刚刚得到了图像的宽度和高度,并计算了纵横比

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);
            }

这比我在解决方案中建议的使用
ControllerListener
效率要低得多。看看fresco不支持ImageView的一系列本机功能,包括
adjustViewBounds
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);
            }