2016-07-20 20 views
0

一個DraweeView的尺寸設置爲「WRAP_CONTENT」我使用Fresco庫在我的應用程序加載圖像。 的問題是,我無法將圖像高度設置爲"wrap_content"因爲"wrap_content"Fresco庫不支持。 那麼,如何使用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" /> 

BTW的圖像尺寸是730 * 760

回答

2

既然您已經知道圖片的確切尺寸,您可以簡單地計算圖片的縱橫比,然後只需在XML或Java中設置縱橫比。 這可以直接使用,不需要其他答案中建議的任何自定義視圖或DP /像素計算。

對於你的榜樣,這將是W/H =七百六十零分之七百三十零= 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見在最底層。

+0

非常感謝! –

1

您可以創建Custom視圖擴展SimpleDraweeView像下面這樣

public class WrapContentDraweeView extends SimpleDraweeView { 

    // we set a listener and update the view's aspect ratio depending on the loaded image 
    private final ControllerListener listener = new BaseControllerListener<ImageInfo>() { 
     @Override 
     public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) { 
      updateViewSize(imageInfo); 
     } 

     @Override 
     public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) { 
      updateViewSize(imageInfo); 
     } 
    }; 

    public WrapContentDraweeView(Context context, GenericDraweeHierarchy hierarchy) { 
     super(context, hierarchy); 
    } 

    public WrapContentDraweeView(Context context) { 
     super(context); 
    } 

    public WrapContentDraweeView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public WrapContentDraweeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public void setImageURI(Uri uri, Object callerContext) { 
     DraweeController controller = ((PipelineDraweeControllerBuilder)getControllerBuilder()) 
       .setControllerListener(listener) 
       .setCallerContext(callerContext) 
       .setUri(uri) 
       .setOldController(getController()) 
       .build(); 
     setController(controller); 
    } 

    void updateViewSize(@Nullable ImageInfo imageInfo) { 
     if (imageInfo != null) { 
      setAspectRatio((float) imageInfo.getWidth()/imageInfo.getHeight()); 
     } 
    } 
} 

,然後用這種方式

<com.example.ui.views.WrapContentDraweeView 
    android:id="@+id/simple_drawee_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

注:請確保您已經應用軟件包的名稱,否則它 不行。

+0

其實我知道我的圖片的實際尺寸。所以我認爲這不是最好的方法。 –

+0

@ItielMaimon如果你知道他們的尺寸適用於它直接.. – Ironman

+0

尺寸以像素爲單位,我不知道有多少dpi的投入? –

2

用於DP到像素和像素與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佈局參數編程。

+0

謝謝你看起來非常好,但我應該在xml文件中的'android:layout_height'放什麼?我不能將它留空 –

+0

如何以編程方式設置draweeview的高度? –

+0

我不確定設置DraweeView的大小,而是你可以做的是,把它放在另一個視圖中說一個LinearLayout並使用它的Params來設置它的高度和寬度到找到的。讓Drawee保持match_parent狀態,並且它會匹配你應該工作得很好的父親界限。 – Kushan