Reputation: 17920
I want to scale Bitmap for Wallpaper set but no effect:| I have this code and all .jpg files in raw folder and array, code:
baton3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputStream tapeta = getResources().openRawResource(textureArrayWin[n]);
Bitmap bitmap = BitmapFactory.decodeStream(tapeta);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int oldwidth= bitmap.getWidth();
int oldheight= bitmap.getHeight();
float skalaszerokosci = ((float) oldwidth) / width;
float skalawysokosci = ((float)oldheight) / height;
Matrix macierz = new Matrix();
macierz.postScale(skalaszerokosci, skalawysokosci);
Bitmap zmieniona = Bitmap.createBitmap(bitmap, 0, 0,
width, height, macierz, true);
try
{
getApplicationContext().setWallpaper(zmieniona);
}
catch(IOException e)
{
e.printStackTrace();
}
}
});
Upvotes: 1
Views: 1991
Reputation: 21
Here's the code:
// Get display dimensions
final DisplayMetrics metrics = getResources().getDisplayMetrics();
final int displayWidth = metrics.widthPixels;
final int displayHeight = metrics.heightPixels;
// Here I'm decoding a resource, just for the example sake
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
// obtain the original Bitmap's dimensions
final int originalWidth = bitmap.getWidth();
final int originalHeight = bitmap.getHeight();
// Obtain the horizontal and vertical scale factors
final float horizontalScaleFactor = (float) originalWidth / (float) displayWidth;
final float verticalScaleFactor = (float) originalHeight / (float) displayHeight;
// Get the biggest scale factor to use in order to maintain original image's aspect ratio
final float scaleFactor = Math.max(verticalScaleFactor, horizontalScaleFactor);
final int finalWidth = (int) (originalWidth / scaleFactor);
final int finalHeight = (int) (originalHeight / scaleFactor);
// Create the final bitmap
final Bitmap wallpaperBmp = Bitmap.createScaledBitmap(
bitmap, finalWidth, finalHeight, true);
// Recycle the original bitmap
bitmap.recycle();
// Finally set it as wallpaper
try {
final WallpaperManager wallMan = WallpaperManager.getInstance(this);
wallMan.setBitmap(wallpaperBmp);
} catch (IOException e) {
Log.e("Wallpaper", "Something went wrong: " + e.toString());
wallpaperBmp.recycle();
}
Also, don't forget to add the SET_WALLPAPER to your manifest.xml:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Upvotes: 1
Reputation: 9
add to manifest
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER" />
Upvotes: 1