views:

25

answers:

1

I'd like to be able to use an image as my background in a relative layout, without it stretching. I don't want to just enter the images details into an XML file, as I want to use different images, with different sizes.

I've tried the following, but it just stretches the images (I know the left, top, right and bottom variables have the wrong values, but I've just entered something into them, as a test to see if it makes any difference to the output, but it does not. If it did, I'd calculate the correct values programmaticly : -

    RelativeLayout explosionlayout = (RelativeLayout) findViewById (R.id.explosionlayout);

     imageAnim = (ImageView) findViewById(R.id.explosion);

   Drawable d;
   d = this.getResources().getDrawable(R.drawable.bomb);
   int left = 10; int top = 10; int right = 20; int bottom = 20;
   d.setBounds(left, top, right, bottom);
  explosionlayout.setBackgroundDrawable(d);

This is obviously wrong. What would be the correct solution?

Thanks.

A: 

Hi,

Set the ImageView's scaleType attribute to "center".

ognian
Thanks for the reply. I've tried the following:- imageAnim.setAdjustViewBounds(true); imageAnim.setScaleType(ImageView.ScaleType.CENTER);But it just gets ignored. I think it's to do with the fact that the ImageView is getting used as the background image. I'm sure if I used it as a normal foreground ImageView it would have worked.
andy_spoo
I did try imageAnim.setScaleType(ImageView.ScaleType.CENTER); on it's own but no luck.
andy_spoo