ImageView测量高度一次然后删除监听器

时间:2015-12-03 14:18:32

标签: android imageview picasso

我不明白为什么我的ImageViewHeight在调用“setBitmap”之后仍然是0,我需要在加载图片的精确位置放置一个标记,所以我必须知道它的大小:

Picasso.with(iv.getContext())
    .load(AppConstants.SERVER_URL + "/api/" + question.getImage().getUrl())
    .config(Bitmap.Config.RGB_565)
    .into(new Target() {@Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        iv.setImageBitmap(bitmap);
        iv.setAdjustViewBounds(true);

        Log.i("renaud", "iv.getMeasuredHeight() : " + iv.getMeasuredHeight());
        Log.i("renaud", "iv.getMeasuredWidth() : " + iv.getMeasuredWidth());

    }

    ...

});

此高度和宽度的日志“0”。

xml(iv对应于“area_iv”):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/area_relative"
        android:layout_width="wrap_content"
        android:minWidth="300dp"
        android:minHeight="200sp"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/area_iv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerInside"
            />

        <ImageView
            android:id="@+id/answer_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RelativeLayout>


</LinearLayout>

-------------------感谢你们,我做到了:

Picasso.with(iv.getContext())
    .load(AppConstants.SERVER_URL + "/api/" + question.getImage().getUrl())
    .config(Bitmap.Config.RGB_565)
    .into(new Target() {@Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        iv.setImageBitmap(bitmap);
        iv.setAdjustViewBounds(true);

        iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Override
            public void onGlobalLayout() {

                Log.i("renaud", "iv.getMeasuredHeight() : " + iv.getMeasuredHeight());
                Log.i("renaud", "iv.getMeasuredWidth() : " + iv.getMeasuredWidth());
                iv.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }



});

0 个答案:

没有答案