有没有code设置壁纸无需裁剪和放大的Android? [英] Is there any code to set wallpaper without cropping and zooming in android?
问题描述
我创建一个画廊的应用程序,我的第一个应用程序,这是我的code
i am creating a gallery app , my first app and this is my code
Bitmap bmd = BitmapFactory.decodeStream(is);
try{
getApplicationContext().setWallpaper(bmd);
}catch(IOException e){
e.printStackTrace();
}
以上code设置壁纸但墙纸被裁剪或放大它得到了设置后! 是否有任何的修改,我可以在上面code做,这样我可以设置壁纸,而不进行缩放或当它被设置裁剪!!!!
The above code sets wallpaper But the wallpaper gets cropped or zoomed after it got set !! Is there any modification i can do in the above code so that i can set wallpaper without zooming or cropping when it is set !!!!
Plzzzz帮助我!在此先感谢: - )
Plzzzz help me out !! Thanks in advance :-)
推荐答案
我迟到回复此。希望它可以帮助你和谁访问你的问题:
I am late to reply this. Hope it helps you and those who visits your question:
在你的情况下,尝试通过调整图片大小的设备有点像这样的:
In your case, try to adjust your picture to device size by somewhat like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 1; // best wallpaper width is twice screen width
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap decodedSampleBitmap = BitmapFactory.decodeFile(path, options);
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
} catch (IOException e) {
Log.e(TAG, "Cannot set image as wallpaper", e);
}
如果上面的code不工作,做一个小的修改:
If above code doesn't work, do a small modification:
...
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
wm.setBitmap(decodedSampleBitmap);
wm.suggestDesiredDimensions(width, height);
} catch (IOException e) {
Log.e(TAG, "Cannot set image as wallpaper", e);
}
和方法 calculateInSampleSize
:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
和记得添加权限:
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
这篇关于有没有code设置壁纸无需裁剪和放大的Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!