【问题标题】:How to set dimensions of a DraweeView to "wrap_content" in the Fresco library如何在 Fresco 库中将 DraweeView 的尺寸设置为“wrap_content”
【发布时间】:2016-11-23 17:07:39
【问题描述】:

我在我的应用程序中使用Fresco 库来加载图像。 问题是我无法将图像高度设置为"wrap_content",因为Fresco 库不支持"wrap_content"。 那么如何使用DraweeView 使我的图像看起来不错呢? 我看了这里,但找不到解决这个问题的方法:frescolib/wrap_content

例如:

<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" />

顺便说一句,图像尺寸为 730*760

【问题讨论】:

    标签: android image fresco android-wrap-content


    【解决方案1】:

    由于您已经知道图像的确切尺寸,您可以简单地计算图像的纵横比,然后只需在 XML 或 Java 中设置纵横比。 这是开箱即用的,您不需要其他答案中建议的任何自定义视图或 DP /像素计算。

    对于您的示例,这将是 w/h = 730/760 = 0.96052631578

    现在您可以设置它: 在 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 -->
    

    或者在 Java 中:

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

    另请参阅最底部的http://frescolib.org/docs/using-drawees-xml.html

    【讨论】:

      【解决方案2】:

      对于所有设备上的 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;
      }
      

      将像素转换为dp:

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

      SimpleDraweeView 不直接允许 wrap_content 作为维度。正如上面的答案清楚地表明的那样,解决方法是扩展类。

      使用公式正确找到您需要的指标并以编程方式为您的draweeview设置布局参数。

      【讨论】:

      • Thank you 看起来很好,但是我应该在 xml 文件中的 android:layout_height 中添加什么?我不能让它空着
      • 如何以编程方式设置draweeview的高度?
      • 我不确定如何设置 DraweeView 的大小,您可以做的是,将其放在另一个视图中,例如 LinearLayout 并使用其 Params 将其高度和宽度设置为找到的。将 Drawee 保持为 match_parent,它将匹配您设置的父边界,这应该会很好地工作。
      • 参见:stackoverflow.com/questions/6798867/… 设置线性布局的边界。像这样设置它并保持内部drawee与父匹配:)
      • 很高兴为您提供帮助 :) 好吧,既然您接受了另一个答案,这至少值得提高 ;)
      猜你喜欢
      • 2018-04-19
      • 2018-06-15
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 2011-12-25
      • 2012-03-16
      相关资源
      最近更新 更多