0

I've decoded an image from the gallery to a bitmap, and i'm using a blur class to blur the bitmap. (Error when trying to blur image using RenderScript support library on android phone)

I want to create a background from the blurred bitmap, but it's being stretched with this code (rl is my RelativeLayout):

  Uri selectedImageUri = data.getData();
          Blur blur = new Blur();
          imagepath = getRealPathFromURI(selectedImageUri);

          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(imagepath, options);
          int pow = 0;
          while (options.outHeight >> pow > options.outHeight / 2 || options.outWidth >> pow > options.outWidth / 2) {
              pow += 1;
          options.inSampleSize = 1 << pow; 
          options.inJustDecodeBounds = false;
          }
         Bitmap bmImg = BitmapFactory.decodeFile(imagepath, options);
         Bitmap BlurredIMG = Blur.doBlur(bmImg, 33, false);
         BitmapDrawable bmp = new BitmapDrawable(BlurredIMG);
         rl.setBackground(bmp);

How to keep the bitmap's aspect ratio and scale it so it will cover both width and height of the device? Do I need to use an ImageView instead? How?

Community
  • 1
  • 1
Ofir
  • 101
  • 1
  • 10

2 Answers2

0

Setting the RelativeLayout background resource might not be the way to go. Instead, you may want to have an ImageView that fills the parent and use scaling. This has worked for me in some cases that sound like yours.

Check out this:

Fit a ImageView (and its src) to the layout width and make its height proportional

which is similar to your problem, but maybe try this:

scale the size of downloaded image to fit the imageview

The point is that you need an ImageView that fills the background and then put other elements on top of it. The standard options for your layout background are not adequate for your needs.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • Thank you for directing me to the answer, I used a frameLayout finally (http://stackoverflow.com/questions/1400782/android-scale-a-drawable-or-background-image) just with centerCrop – Ofir Aug 09 '14 at 09:24
0

I found the answer on other question (Android: Scale a Drawable or background image?) and I used centerCrop for the scale type.

    <FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/BG"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop" />

</FrameLayout>

I didn't think that the answer will be so simple!

Community
  • 1
  • 1
Ofir
  • 101
  • 1
  • 10