0

Hie. I am working on the live wallpaper and I have got a problem. I am done with the parallax effect in my wallpaper. Now, the problem is the bitmap (i.e., the static background) of my live wallpaper, is not getting scaled properly. In some screens the width is proper but in some the bitmap (i.e., the background) appears only half way.

I have tried the density, windowmanager and the px to dp conversion.

None of them seem to work for me. Or may be my approach towards it is not in a proper manner.

I need help for the same.

Code Snippet

this._backgroundImage = BitmapFactory.decodeResource(context.getResources(),R.drawable.scene, options);

Bitmap background_image = Bitmap.createScaledBitmap(_backgroundImage, width, height, false);

canvas.drawBitmap(this.background_image, 0, 0, null);
android developer
  • 1,253
  • 2
  • 13
  • 43

3 Answers3

1

I was using following methods sometimes back.. I dont know if these will be helpful for you or not .. please check if this works for you

float scale = getResources().getDisplayMetrics().density;
Display display = ((WindowManager) main.getSystemService(Context.WINDOW_SERVICE)) 
.getDefaultDisplay();
int WIDTH = display.getWidth();
int HEIGHT = display.getHeight();

public static Drawable resizeDrawable(Drawable d, float scale) {
    Drawable drawable = null;
    if (d != null) {
        try {
            Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap();
            int width = 0;
            int height = 0;
            if (Math.min(WIDTH, HEIGHT) > 600) {
                width = (int) (100 * scale + 0.5f);
                height = (int) (100 * scale + 0.5f);

            } else if (Math.min(WIDTH, HEIGHT) > 240) {
                width = (int) (70 * scale + 0.5f);
                height = (int) (70 * scale + 0.5f);

            } else {
                width = (int) (44 * scale + 0.5f);
                height = (int) (44 * scale + 0.5f);
            }

            drawable = new BitmapDrawable(resizeBitmap(bitmap1,
                    width, height));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return drawable;
}

please note that value used in if-else conditions in resizeDrawable method are just arbitrary values taken by trial n error (which suits my app).. you can try other values according to screens you are targeting

public static Bitmap resizeBitmap(final Bitmap bitmap, final int width,
        final int height) {
    final int oldWidth = bitmap.getWidth();
    final int oldHeight = bitmap.getHeight();
    final int newWidth = width;
    final int newHeight = height;

    // calculate the scale
    final float scaleWidth = ((float) newWidth) / oldWidth;
    final float scaleHeight = ((float) newHeight) / oldHeight;

    // create a matrix for the manipulation
    final Matrix matrix = new Matrix();
    // resize the Bitmap
    matrix.postScale(scaleWidth, scaleHeight);
    // if you want to rotate the Bitmap

    // recreate the new Bitmap
    final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            oldWidth, oldHeight, matrix, true);

    return resizedBitmap;
}
silwar
  • 6,470
  • 3
  • 46
  • 66
0

Try this:

Bitmap resizedBitmap=Bitmap.createScaledBitmap(bb, newWidth, newHeight, false);

Tell me if it works.

Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
0

You can use this to get the screen size and scale it accordingly.

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47