Reputation: 1071
Hi i am coding to set a Home Screen Wallpaper. It's working fine. But my image pixel is totally damaged and then my wallpaper is not fit with the actual size of the home screen. I am try to workout different size of images. Unfortunately it's not working for me. How to solve it.
My code is here
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable drawable = getResources().getDrawable(R.drawable.newimage);
Bitmap wallpaper = ((BitmapDrawable) drawable).getBitmap();
try
{
wallpaperManager.setBitmap(wallpaper);
}
catch (IOException e)
{
e.printStackTrace();
}
My Screenshot Original Image
My Screenshot Android Emulator Home Screen
Why my original image is damaged here.
How to display My Original Image
based on the Emulator Size
.
Upvotes: 9
Views: 1339
Reputation: 347
You can try this:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable drawable = getResources().getDrawable(R.drawable.newimage);
Bitmap wallpaper_source = ((BitmapDrawable) drawable).getBitmap();
try {
int w = wallpaperManager.getDesiredMinimumWidth();
int h = wallpaperManager.getDesiredMinimumHeight();
int x = (w-wallpaper_source.getWidth())/2;
int y = (h-wallpaper_source.getHeight())/2;
Bitmap wallpaper = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas c = new Canvas(wallpaper);
c.drawBitmap(wallpaper_source, x,y, null);
wallpaperManager.setBitmap(wallpaper);
}
catch (IOException e)
{
e.printStackTrace();
}
Upvotes: 7
Reputation: 14377
WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
myOptions.inDither = false;
myOptions.inPurgeable = true;
Bitmap preparedBitmap = BitmapFactory.decodeResource(activity
.getApplication().getResources(), R.drawable.newimage, myOptions);
try
{
wallpaperManager.setBitmap(preparedBitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
This is what I use to make my images scale well without crazy lines running across - you could try this for wallpaper - not sure if it works , let me know.
Upvotes: 0