2013-03-19 21 views
0

我想高效地載入位圖,因爲經常在4.0+安卓上我得到OutOfMemoryError。所以我試圖從android開發者頁面資源「有效地加載位圖」中使用示例代碼。android有效載入位圖

但是我的問題是獲取ImageView的高度和寬度,因爲沒有任何常量值來定義尺寸。

代碼與開發者頁面中的必填字段。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) 

這裏是我的佈局:

<RelativeLayout android:layout_width="match_parent" 
       android:layout_weight="3" 
       android:layout_height="0px"> 

    <ImageView 
    android:id="@+id/img1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:scaleType="fitStart" 
    android:src="@drawable/image" /> 

我試圖用image.getHeight()但沒有工作。

+0

應搜索第一;) [鏈接](http://stackoverflow.com/questions/4142090/how-do-you-to-retrieve-dimensions-of- a-view-getheight-and-getwidth-always-r/4406090#4406090) – 2013-03-19 22:04:38

+0

我沒有反對意見,但是那個鏈接並不能解決問題。你有一個'ImageView',你將寬度和高度設置爲'wrap_content',意思是「使視圖適合圖像的大小」,但是你試圖調整圖像的大小以適應視圖...循環邏輯;它不會工作。您需要選擇尺寸並根據圖像調整尺寸 – kabuko 2013-03-19 22:12:12

回答

0

這是因爲'圖像'不是ImageView對象。下面是如何以編程方式在您的情況下聲明ImageView:

`ImageView image =(ImageView)findViewById(R.id.img1);

而且,這可能幫助你:How to retrieve the dimensions of a view?