【问题标题】:How to get the size of imageView that is set to match_parent?如何获取设置为 match_parent 的 imageView 的大小?
【发布时间】:2012-12-20 15:16:22
【问题描述】:

所以我在 listview 中有一个 imageView(由 Loader 加载),并且宽度设置为 match_parent。当我想设置 imageview 位图时,我使用 imageView 的高度和宽度来加载最佳大小的位图。问题是 ImageView 大小有时在 Loader bindView 中是未知的(尚未测量?)。在这种情况下,我怎么能每次都得到位图的宽度?

@Override
    public void bindView(View view, final Context context, Cursor cursor) {
        ImageView iv = (ImageView) (ImageView) view.findViewById(R.id.image);
        ....
        bitmap = BitmapHelper.decodeSampledBitmapFromFile( new File(file), iv.getWidth(), iv.getHeight());
        imageView.setImageBitmap(bitmap);
    }

【问题讨论】:

标签: android imageview


【解决方案1】:

您必须等到视图初始化后,才能获取尺寸。尝试像这样使用 ViewObserver:

ViewTreeObserver viewTreeObserver = YOURLAYOUT.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
   viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
          public void onGlobalLayout() {
             YOURLAYOUT.getViewTreeObserver().removeGlobalOnLayoutListener(this);
             //do some things
             //YOURLAYOUT.getWidth() should give a correct answer
          }
   });
}

希望有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多