首先我有一个自定义组件,它显示来自网络的图片。这是一个简单的布局,包括一个圆形进度条和一个 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。它提供了很好的价值,但它稍后运行,解码后:(
如何计算我的布局大小?