Android 有没有代码可以在安卓系统中设置墙纸而不进行裁剪和缩放?
我正在创建一个画廊应用程序,这是我的第一个应用程序,这是我的代码Android 有没有代码可以在安卓系统中设置墙纸而不进行裁剪和缩放?,android,set,zooming,crop,wallpaper,Android,Set,Zooming,Crop,Wallpaper,我正在创建一个画廊应用程序,这是我的第一个应用程序,这是我的代码 Bitmap bmd = BitmapFactory.decodeStream(is); try{ getApplicationContext().setWallpaper(bmd); }catch(IOException e){ e.printStackTrace(); } 上面的代码设置墙纸,但墙纸设置后会被裁剪或缩放!! 是否有任何修改,我可以在上面的代码,
Bitmap bmd = BitmapFactory.decodeStream(is);
try{
getApplicationContext().setWallpaper(bmd);
}catch(IOException e){
e.printStackTrace();
}
上面的代码设置墙纸,但墙纸设置后会被裁剪或缩放!!
是否有任何修改,我可以在上面的代码,使我可以设置墙纸,而不缩放或裁剪时,它是设置
帮帮我!!提前感谢:-)我来不及回复了。希望它能帮助您和访问您的问题的人: 在您的情况下,尝试通过以下方式将图片调整为设备大小:
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);
}
方法计算样本大小:
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"/>
最简单的方法是将壁纸图片展开到nn正方形,将内容放在新图片的中心。
如果您的设备是w1024h600,您的壁纸应该是10241024。
如果您的设备是w600h1024,您的壁纸也应该是1024*1024。您想将位图大小设置为设备显示的大小,还是想实现视差效果?什么是calculateInSampleSize方法。请解释一下。不起作用,它是按高度裁剪的。但如何设置壁纸的全宽位图?
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>