How do I find the width/height of ImageView?
Asked Answered
S

2

0

My question is similar to this question. I've used this code to extend ImageView to form TouchImageView. In my onCreate() method, I want to call

setImage(Bitmap bm, int displayWidth, int displayHeight)

but I don't know the width or height to use. When I call getHeight(), it gives me zero.

I tried to avoid the 0 using kcoppock's answer to the previously mentioned question, but was unsuccessful. The onPreDraw() listener is called several times per second and I can't figure out how to only call it once.

Specious answered 5/7, 2011 at 23:32 Comment(0)
C
3

That's a brittle approach for something like this. Android Views are measured by their parent View using the onMeasure method. By providing an initial size to a setImage method to scale to (and especially if you set it based on the display size) you're inviting complexity whenever you want to nest this View within others. Larger screen devices like tablets are a good example of this.

A more idiomatic approach would implement the setImage method of TouchImageView without the width/height parameters at all. Instead it would defer setting the scale and matrices until measurement and implement onMeasure something like this:

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;
    }
}

Where the setInitialScale method does everything that the linked setImage method does after the call to super.setImageBitmap. setImage should also set the mNeedsInitialScale field to true.

This way you will never need to worry about the initial size to use, it will be obtained automatically from the View during normal measurement whenever a new image is set.

Chowder answered 6/7, 2011 at 0:37 Comment(4)
Thanks for your answer. That solved my problem and my code is much better now.Specious
Actually, it looked like it was working well, but getMeasuredWidth() and getMeasuredHeight() seem to be returning the width and height of the parent view.Specious
It turns out that onMeasure(int int) gets called several times to measure the ImageView. I expect it gets called whenever a sibling is added. I moved mNeedsInitialScale = false; to the beginning of my setScale(float) method and now it works fine.Specious
Yes, onMeasure will be called whenever a View is asked to measure with different MeasureSpecs than it used last time. This can happen whenever a layout is requested. Glad it worked out. :)Chowder
H
1

the ImageViews dimensions are 0 until your Activity draw it on the screen. You could use Activitys onFocusChanged to set the Bitmap and get correct Dimensions if you are just looking for DisplayWidth and height you can call

Display d = getSystemService(WINDOW_SERVICE).getDefaultDisplay();

and ask Display for its dimensions

Holbrooke answered 5/7, 2011 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.