简体   繁体   English

如何在Fresco库中将DraweeView的尺寸设置为“ wrap_content”

[英]How to set dimensions of a DraweeView to “wrap_content” in the Fresco library

I'm using the Fresco library in my app to load images. 我在我的应用程序中使用Fresco库加载图像。 The problem is that I can't set my images height to "wrap_content" because "wrap_content" is not supported in the Fresco library. 问题是我无法将图像高度设置为"wrap_content"因为Fresco库中不支持"wrap_content" So how can I make my images to look good using a DraweeView ? 那么如何使用DraweeView使图像看起来更好呢? I looked here but I couldn't find a solution to this problem : frescolib/wrap_content 我在这里看过,但找不到解决此问题的方法: frescolib / wrap_content

For example: 例如:

<com.facebook.drawee.view.SimpleDraweeView
         android:id="@+id/intro_image1"
         android:layout_width="match_parent"
         android:layout_height= // Can't use "wrap_content"
         fresco:placeholderImage="@drawable/placeholder_image" />

BTW the images dimensions are 730*760 顺便说一句,图像尺寸为730 * 760

Since you already know the exact dimensions of the image, you can simply calculate the aspect ratio of your image and then just set the aspect ratio in XML or Java. 由于您已经知道图像的确切尺寸,因此您可以简单地计算图像的长宽比,然后只需以XML或Java设置长宽比即可。 This works out-of-the-box and you don't need any custom views or DP / pixel calculations as suggested in other answers. 这是开箱即用的,不需要其他答案中建议的任何自定义视图或DP /像素计算。

For your example, this would be w/h = 730/760 = 0.96052631578 对于您的示例,这将是w / h = 730/760 = 0.96052631578

Now you can just set it: In XML: 现在您可以进行设置:在XML中:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/my_image_view"
    android:layout_width="match_parent" <!-- or the desired width -->
    android:layout_height="wrap_content"
    fresco:viewAspectRatio="0.96052631578" <!-- your aspect ratio -->
    <!-- other attributes -->

Or in Java: 或在Java中:

mSimpleDraweeView.setAspectRatio(0.96052631578f); // or whatever your aspect ratio is

See also http://frescolib.org/docs/using-drawees-xml.html at the very bottom. 另请参阅底部的http://frescolib.org/docs/using-drawees-xml.html

For dp to px and px to dp on all devices, use the following formula: 对于所有设备上的dp到px和px到dp,请使用以下公式:

Convert dp to pixel:

public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
return px;
}

Convert pixel to dp: 将像素转换为dp:

public int pxToDp(int px) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
return dp;
}

SimpleDraweeView does not directly allow wrap_content as dimension. SimpleDraweeView不允许直接将wrap_content用作维度。 The work around is to extend the class as the answer above clearly indicates. 解决方法是扩展类,如上面的答案明确指出的那样。

Use the formula to correctly find your needed metric and set the Layout parameters for your draweeview programmatically. 使用公式正确找到所需的度量标准,并以编程方式为draweeview设置Layout参数。

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

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