Orbit
Orbit

Reputation: 2385

Measuring an ImageView after loading an image

Im trying to find a way to measure an ImageView after an image has been loaded into it using Glide or Picasso (or anything really). Basically, im trying to layout other views on top of the image in certain positions, but need the final ImageViews dimensions to do so accurately.

Im not sure what the best layout to use to try and do this would be, but im currently using this:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/viewingImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </FrameLayout>

and loading the image into viewingImageView. These are both inside a root FrameLayout as well but I dont think that matters.

This is my latest attempt, but as commented, using .getWidth() and .getHeight() on the ImageView return 0. The width/height of the resource returns the size of the original image.

Glide

.with(this)
.load(entry.getImageUrl())
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        mImageView.setImageBitmap(resource);

        int width = mImageView.getMaxWidth(); //prints 0
        int height = mImageView.getMaxHeight(); //prints 0
        int resw = resource.getWidth(); //returns original image width
    }
});

So how can I get the image loaded and then measure the ImageView (or its wrapping FrameLayout) after the image has been loaded? Or if possible, measuring the dimensions of the finally laid out image would be better, as I know the image doesnt always fill the entire ImageView depending on scale types. I'm open to any solutions, the above is only what ive tried so far.

Upvotes: 4

Views: 3675

Answers (2)

Marko
Marko

Reputation: 20513

You should wait for the view to be drawn, you can use OnGlobalLayoutListener like this

ViewTreeObserver vto = mImageView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        this.mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

        // Get the width and height
        int width  = mImageView.getMeasuredWidth();
        int height = mImageView.getMeasuredHeight(); 
    } 
});

Upvotes: 6

Ramesh sambu
Ramesh sambu

Reputation: 3539

Create a custom class with image view. Use this onMeasure in the image view we can get the height and width

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (mNeedsInitialScale) { // Set by setImage when a new image is set
    // The measured width and height were set by super.onMeasure above
    setInitialScale(getMeasuredWidth(), getMeasuredHeight());
    mNeedsInitialScale = false;
}

}

Upvotes: 1

Related Questions