views:

207

answers:

7

Hello,

I'm trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I'm looking for this new size.

I've tried to override the onDraw method of the ImageView in a custom view but I'm not getting the correct height and width...

public class LandImageView extends ImageView
{
    public LandImageView( Context context )
    {
        super( context );
    }

    public LandImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public LandImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw( Canvas canvas )
    {
        super.onDraw( canvas );

        int test = this.getWidth();
        int test2 = this.getHeight();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}

Do you have any clues ?

A: 

try overriding onMeasure(int widthMeasureSpec, int heightMeasureSpec) instead of onSizeChanged.

bhups
Even with this method, I have not the data I need.
Guillaume Ménant
Even with the method onMeasure I have not the display size of the bitmp but the size of the layer I guess...
Guillaume Ménant
A: 

No answer for this "basic" request ? Perhaps I'm not taking the problem from the good side...

In fact my need is to place on this image some markers and their coordinates are based on the real image size. So I would know the diplay size to place my markers on the screen. These marquers have to be clickable so I can't add them on the image before displaying it.

Do you have other ideas to do this without this size ?

Guillaume Ménant
A: 

How about ImageView.getBackground() or ImageView.getDrawable(), then Drawable.getBounds()?

rreeverb
A: 

Try

ImageView.getDrawable().getIntrinsicHeight()
ImageView.getDrawable().getIntrinsicWidth()
Peter Knego
A: 

If I get you correctly you need your ImageView dimension, in order to scale your image accordingly. I did this with a custom class, where I override the onMeasure() call to get width and height.

class LandImageView extends ImageView{ 
public LandImageView (Context context, AttributeSet attrs) {
  super (context, attrs);
 } 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
 {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  final int width = MeasureSpec.getSize(widthMeasureSpec);
  final int height = MeasureSpec.getSize(heightMeasureSpec);

  Log.v("", String.format("w %d h %d", width, height));
                // width and height are the dimensions for your image
                // you should remember the values to scale your image later on

  this.setMeasuredDimension(width, height );
 }}

In onMeasure you get the width and height for your image so that it fits your view.

You can use the LandImageView in your Layout like this:

<my.package.LandImageView ... >
pivotnig
A: 

Try loading your resource using [BitmapFactory.decodeResource (Resources res, int id, BitmapFactory.Options opts)][1] with the BitmapFactory.Options() class. BitmapFactory.Options() has a flag which called "inJustDecodeBounds" and gives only the resource dimensions.

Hope that helped.

[1]: http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)

Uri May
A: 

Hi, I'm just passing by but hope it still helps I'm going under the assumption your talking about bitmaps in your imageView

what you want to understand is the difference between

bmpBack.getWidth() -> this gives you the size of your bitmap and bmpBack.getScaledWidth(canvas); -> this will give you the size of the bitmap as displayed on the screen.

I never used ImageView because the relative display was driving me mad so in the end I just override the onDraw and did my canvas very similarly to opengl.

I think this is your problem

cheers

Jason