How do I find the width/height of ImageView?

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.

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.

Thanks for your answer. That solved my problem and my code is much better now. – GregNash Jul 7 at 22:28 Actually, it looked like it was working well, but getMeasuredWidth() and getMeasuredHeight() seem to be returning the width and height of the parent view.

– GregNash Jul 7 at 23:17 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.

– GregNash Jul 8 at 0:31 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.

:) – adamp Jul 9 at 18:08.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions