我必须从URL或文件名/ Uri中将图像加载到SimpleDraweeView
。
这是我的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" />
当我从Facebook或Instagram等网址加载图片时,我会获得图片的width
和height
,并根据该图像计算宽高比并将其设置为{{ 1}}像这样:
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);
或SimpleDraweeView
以正确的宽高比将图片加载到filename
,我该怎么做?
答案 0 :(得分:0)
一般情况下,我们不鼓励人们使用这种方法,因为Drawee可能会显示超过1件事,并且没有真正的内在规模。 Read more about the reasons why wrap_content
is not supported here。此页面在最后一段中还有一个链接如何controller listener can be used for this。
但是,由于Fresco文档中列出的所有问题,最好考虑采用不同的方法。例如,固定大小和适当的比例类型可以更好地工作。
答案 1 :(得分:0)
试试这个:
设置adjustViewBounds = true
答案 2 :(得分:-1)
解决了它。只需获得图像的宽度和高度,然后计算宽高比。
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);
}