views:

548

answers:

1

. . I have more than one question, but I'll start with the more important and problematic one:

. . I have a FrameLayout with a ImageView inside it. . . I need to get the size of the "usable area" of the screen my activity is ocupping, so I set the "onSizeChanged" on my View (I extended the ImageView class). Everything worked fine here. . . Now I have a 1000x1000 image that I want to show on the screen, but without scaling. I want it to be clipped, really. If I set the "ImageView" dimensions using "viewObject.setLayoutParams(new Gallery.LayoutParams(1000, 1000));" I get the image being showed correctly, but then the "onSizeChanged" event returns me always the "1000 x 1000" custom size rather than the real screen size value. . . Any ideas of how can I show an image with its real size (no scale!) and still get the view to report the screen available space? I can change the layout as needed as well, of course.

. Amplexos.

A: 

Are you asking to get the dimensions of the ImageView? If so then you can get that using getLocalVisibleRect. Here's roughly how it's done:

ImageView yourImageView;

public void onCreate(...){
    setContentView(...)
    yourImageView = (ImageView) findViewById(...);
    (...)
}

getImageViewSize(){
    Rect imageViewSize = new Rect();
    yourImageView.getLocalVisibleRect(imageViewSize);
    // imageViewSize now has all the values you need
    Log.d("Your log tag", "ImageView width = " + (imageViewSize.right -
            imageViewSize.left));
}

There is however a catch. You have to make sure that you don't try to get the size of the view until after view is finished being laid out on the screen. In other words, if you try to get its size in onCreate, its size will be 0. You have to get it afterwards, for example at the same time as you resize your image, assuming that's done with a button. (If you're using a SurfaceHolder you can also call it during the surfaceCreated callback, but I doubt you're using one of those...)

Steve H