0

I am trying to do a blurred background for an activity with linearlayout with a BitmapDrawable. The original and blurred bitmaps are like 100x100. I want to scale this uniformly for the phone resolution ( eg. 1080p which isn't a square). I get the background icon at runtime.

    LinearLayout layout = (LinearLayout) findViewById(R.id.mylinearlayout);

    Bitmap bmp = imgLoader.getBitmap(iconUrl);

    //Blur using this thread http://stackoverflow.com/questions/14780006/transparent-blurry-view-which-blurs-layout-underneath
    Bitmap blurredBmp = CommonUtils.blurBitmap(bmp, Constants.RADIUS);
    BitmapDrawable bmpdrawable = new BitmapDrawable(getResources(),blurredBmp);
    bmpdrawable.setAlpha(Constants.ALPHA);

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        layout.setBackgroundDrawable(bmpdrawable);
    } else {
        layout.setBackground(bmpdrawable);
    } 

Now, the issue : when I set the background, the 100x100 is basically getting stretched to non-square phone resolution and it is looking weird.

What I want : fill the entire activity background with stretched bitmap. It's OK if part of it is clipped but aspect ratio needs to be maintained.

What I tried: lot of threads on setting layout background suggest using imageview as one of the elements + use gravity and fill_parent. I don't want to do this since I have other images to show on the screen.

I tried Bitmap.createScaledBitmap when creating blurred bitmap or sending varied width/height it isn't working. any help will be appreciated.

Just marking this as a dupe of another thread without understanding the issue isn't cool as well.

thanks

techtinkerer
  • 1,280
  • 2
  • 15
  • 26
  • extend BitmapDrawable and do your custom drawing in `draw(Canvas)` method, the easiest is to use Canvas.drawBitmap with a Matrix parameter – pskink Dec 20 '14 at 10:21
  • and dont use any Bitmap.createScaledBitmap, scale your Canvas instead (either directly or indirectly) – pskink Dec 20 '14 at 10:42

2 Answers2

0

OK, this worked.

    LinearLayout layout = (LinearLayout) findViewById(R.id.testreportlinearlayout);

    Bitmap bmp = imgLoader.getBitmap(iconUrl);
    Bitmap blurredBmp = CommonUtils.blurBitmap(bmp, Constants.RADIUS);

    Bitmap scaledBmp = Bitmap.createScaledBitmap(blurredBmp, MAX_OF_NEW_XY, MAX_OF_NEW_XY, false);
    BitmapDrawable scaledBmpdrawable = new BitmapDrawable(getResources(), scaledBmp);
    scaledBmpdrawable.setAlpha(Constants.ALPHA);
    scaledBmpdrawable.setGravity(Gravity.CENTER);

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        layout.setBackgroundDrawable(scaledBmpdrawable);
    } else {
        layout.setBackground(scaledBmpdrawable);
    }
techtinkerer
  • 1,280
  • 2
  • 15
  • 26
  • this is a bad solution: don't use Bitmap.createScaledBitmap as it increases your memory usage, instead of Bitmap scaling use Canvas Scaling – pskink Dec 20 '14 at 10:47
0

There are many solution for this issue.

when you have only set as color

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);
Yazan
  • 6,074
  • 1
  • 19
  • 33
darshan
  • 1
  • 3