开发者

ImageView bitmap scale dimensions

开发者 https://www.devze.com 2023-02-10 23:05 出处:网络
I have a Bitmap th开发者_如何学运维at is larger than the ImageView that I\'m putting it in. I have the ScaleType set to center_inside. How do I get the dimensions of the scaled down image?Ok. I probab

I have a Bitmap th开发者_如何学运维at is larger than the ImageView that I'm putting it in. I have the ScaleType set to center_inside. How do I get the dimensions of the scaled down image?


Ok. I probably should have been clearer. I needed the height and width of the scaled Bitmap before it's ever drawn to the screen so that I could draw some overlays in the correct position. I knew the position of the overlays in the original Bitmap but not the scaled. I figured out some simple formulas to calculate where they should go on the scaled Bitmap.

I'll explain what I did in case someone else may one day need this.

I got the original width and height of the Bitmap. The ImageView's height and width are hard-coded in the xml file at 335.

int bitmap_width = bmp.getWidth();
int bitmap_height = bmp.getHeight();

I determined which one was larger so that I could correctly figure out which one to base the calculations off of. For my current example, width is larger. Since the width was scaled down to the the width of the ImageView, I have to find the scaled down height. I just multiplied the ratio of the ImageView's width to the Bitmap's width times the Bitmap's height. Division is done last because Integer division first would have resulted in an answer of 0.

int scaled_height = image_view_width * bitmap_height / bitmap_width;

With the scaled height I can determine the amount of blank space on either side of the scaled Bitmap by using:

int blank_space_buffer = (image_view_height - scaled_height) / 2;

To determine the x and y coordinates of where the overlay should go on the scaled Bitmap I have to use the original coordinates and these calculated numbers. The x coordinate in this example is easy. Since the scaled width is the width of the ImageView, I just have to multiply the ratio of the ImageView's width to the Bitmap's width with the original x coordinate.

int new_x_coord = image_view_width * start_x / bitmap_width;

The y coordinate is a bit trickier. Take the ratio of the Bitmap's scaled height to the Bitmap's original height. Multiply that value with the original y coordinate. Then add the blank area buffer.

int new_y_coord = scaled_height * start_y / bitmap_height + blank_space_buffer;

This works for what I need. If the height is greater than the width, just reverse the width and height variables.


Here's how I do it:

ImageView iv = (ImageView)findViewById(R.id.imageview);
Rect bounds = iv.getDrawable().getBounds();
int scaledHeight = bounds.height();
int scaledWidth = bounds.width();

You can use Drawable's getIntrinsicWidth() or height method if you want to get the original size.

EDIT: Okay, if you're trying to access these bounds at the time onCreate runs, you'll have to wait and retrieve them till after the layout pass. While I don't know that this is the best way, this is how I've been able to accomplish it. Basically, add a listener to the ImageView, and get your dimensions just before it's drawn to the screen. If you need to make any layout changes from the dimensions you retrieve, you should do it within onPreDraw().

ImageView iv = (ImageView)findViewById(R.id.imageview);
int scaledHeight, scaledWidth;
iv.getViewTreeObserver().addOnPreDrawListener(
    new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        Rect rect = iv.getDrawable().getBounds();
        scaledHeight = rect.height();
        scaledWidth = rect.width();
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        return true;
    }
});


Try this code:

final ImageView iv = (ImageView) findViewById(R.id.myImageView); 
ViewTreeObserver vto = iv.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        Toast.makeText(MyActivity.this, iv.getWidth() + " x " + iv.getHeight(), Toast.LENGTH_LONG).show(); 
        iv.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    }  
});  

Otherwise you can use onSizeChange() by making this view a custom one.

0

精彩评论

暂无评论...
验证码 换一张
取 消