android高效加载位图

android高效加载位图,android,bitmap,dimensions,Android,Bitmap,Dimensions,我想高效地加载位图,因为在4.0+android上,我经常会遇到OutOfMemoryError。所以我尝试使用android开发者页面资源“高效加载位图”中的示例代码 然而,我的问题是获得ImageView的高度和宽度,因为没有任何常量值来定义尺寸 使用“开发人员”页面中的必填字段编写代码 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, in

我想高效地加载位图,因为在4.0+android上,我经常会遇到
OutOfMemoryError
。所以我尝试使用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()
,但没有成功。

这是因为“image”不是ImageView对象。以下是如何在您的案例中以编程方式声明ImageView:

`ImageView图像=(ImageView)findViewById(R.id.img1)


此外,这可能会对您有所帮助:

您可以使用ViewTreeObserver并等待其中一个回调,然后获取ImageView的大小

请参阅此处的更多信息:

应首先搜索;)我不反对先搜索,但是这个链接并不能解决问题。您有一个
ImageView
,您正在将其宽度和高度设置为
wrap_content
,意思是“使视图适合图像大小”,但随后您正在尝试根据视图调整图像大小。。。循环逻辑;这行不通。您需要选择尺寸并调整图像的大小