0

I'm trying to use WallpaperManager to FIT CENTER a bitmap to the wallpaper of my phone.

  • Image size: 3840 x 2160
  • Phone size: 1080 x 1920

I've tried numerous strategies:

  1. myWallpaperManager.suggestDesiredDimensions(width, height);
  2. Bitmap.createScaledBitmap(mImage, width, height, true);
  3. Set Wallpaper with bitmap avoid crop and set fit center
  4. how to fit the whole image on screen as wallpaper
  5. Wallpaper not properly fit on device screen
  6. https://developer.android.com/reference/android/app/WallpaperManager.html
  7. http://androidexample.com/How_to_Set_WallPaper_in_Android/question_answer.php?view=qad&token=39

Every time I get a weird bitmap that doesn't fit center, does anyone have any suggestions?

What I want it to look like: enter image description here

Image info: enter image description here

SeaRoth
  • 177
  • 5
  • 17

1 Answers1

2

I found the answer: Scaled Bitmap maintaining aspect ratio

What I did was this:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int phoneHeight = metrics.heightPixels;
    int phoneWidth = metrics.widthPixels;

    Bitmap mBit = returnBitmap(mImage, phoneWidth, phoneHeight);

private Bitmap returnBitmap(Bitmap originalImage, int width, int height){
    Bitmap background = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);

    float originalWidth = originalImage.getWidth();
    float originalHeight = originalImage.getHeight();

    Canvas canvas = new Canvas(background);

    float scale = width / originalWidth;

    float xTranslation = 0.0f;
    float yTranslation = (height - originalHeight * scale) / 2.0f;

    Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);

    Paint paint = new Paint();
    paint.setFilterBitmap(true);

    canvas.drawBitmap(originalImage, transformation, paint);

    return background;
}
SeaRoth
  • 177
  • 5
  • 17
  • Thanks, After Searching for a week to set a wallpaper that fits the screen, Now I Found this Answer. Thank you very much. It helped me a lot. – Ramesh Jul 27 '19 at 18:23