【问题标题】:android loading bitmap efficientlyandroid有效加载位图
【发布时间】:2013-03-19 22:01:15
【问题描述】:

我想有效地加载位图,因为经常在 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(),但没用。

【问题讨论】:

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

标签: android bitmap dimensions


【解决方案1】:

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

在此处查看更多信息: When Can I First Measure a View?

【讨论】:

    【解决方案2】:

    这是因为 'image' 不是 ImageView 对象。以下是如何在您的情况下以编程方式声明 ImageView:

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

    此外,这可能会对您有所帮助:How to retrieve the dimensions of a view?

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-18
      相关资源
      最近更新 更多