0

首先我有一个自定义组件,它显示来自网络的图片。这是一个简单的布局,包括一个圆形进度条和一个 ImageView。在默认状态下,进度条是可见的,它在图片下载时显示,完成后我隐藏进度条并在 ImageView 中显示图片。它工作得很好,但是在模拟器和 HTC Hero 上我得到了java.lang.OutOfMemoryError: bitmap size exceeded VM budget错误。我在这里找到了解决方案。但我的问题是,TARGET_WIDTH 和 TARGET_HEIGHT 无法修复,有时是 60x90,有时是 fill_parent x fill_parent,我无法在解码函数中计算这些值。例如,我使用 layout_width="fill_parent" 在 xml 中添加了我的视图并在 Activity 中使用

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageLoader picture = (ImageLoader) findViewById(R.id.picture);
picture.load("http://winportal.net/images/galleries/wallpapers/Earth.jpg");

在加载方法中,我从解决方案中调用解码函数并尝试在这里计算大小:

...
this.onMeasure(0, 0);
Log.d("Log", "Layout width: " + this.getMeasuredWidth());

TARGET_WIDTH =  this.getMeasuredWidth();
...

结果是:“布局宽度:24

我不明白为什么得到 24 是因为进度条的宽度,但布局 - 包括它 - 是 fill_parent 宽度。我尝试了覆盖 onMeasure 方法,但得到了 0。

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(parentWidth, parentHeight);
} 

我也尝试过覆盖 onSizeChanged。它提供了很好的价值,但它稍后运行,解码后:(

如何计算我的布局大小?

4

1 回答 1

0

当你调用“this.onMeasure(0, 0);” 您强制视图使用以下规则进行测量 -未指定。您的视图具有以下状态:ProgressBar 可见,ImageView 隐藏,因此结果将为 24。

一开始,尝试将FrameLayout与 ProgressBar 和 ImageView 一起使用。在那种情况下,您不必关心测量。只需确保图像不会太大(在 OutOfMemory 的情况下)。您可以将图像文件下载到临时文件,然后检查大小,计算适当的采样大小并进行解码,为 ImageView 设置位图,为 ImageView 设置比例类型

于 2011-04-07T08:48:51.733 回答