1

The code I am using to set a background to a bitmap is

wallpaperManager.setBitmap(result, null, true, WallpaperManager.FLAG_SYSTEM);
wallpaperManager2.setBitmap(result, null, true, WallpaperManager.FLAG_LOCK);

However, the image is not centered and I believe it has to do with the null parameter, which accepts a Rect as visibleCropHint.

How would I set the result bitmap to be centered by X and Y?

Bryan W
  • 1,112
  • 15
  • 26

1 Answers1

0

As doc says:

Passing null for this parameter means that the full image should be displayed if possible given the image's and device's aspect ratios, etc.

So can't guarantee image displayed fully.

Try this(from here):

Bitmap img = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.paper));

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels; 
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(img, width, height, true); 

WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); 
 try {
  wallpaperManager.setBitmap(bitmap);
 } catch (IOException e) {
  e.printStackTrace();
 }
navylover
  • 12,383
  • 5
  • 28
  • 41
  • This works, however, I don't want to distort the image. The above stretches the image to fit within the display, but I only want to position the image centered and crop what doesn't fit. Is this possible with `createScaledBitmap`? – Bryan W Oct 10 '18 at 04:45