我正在开发一个应用程序,它从图库中选择一个图像,然后将该图像设置为壁纸。但这里的问题是只有部分图像被设置为壁纸而不是整个图像,但我想将整个图像设置为壁纸。你能告诉我怎么做吗?
这是我的代码......
public class Scaleimage extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String FileName;
File file = new File("/sdcard/pictures");
File[] imageFiles = file.listFiles( );
if(imageFiles.length > 0 ) {
FileName = imageFiles[0].getName();
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
Bitmap myBitmap = BitmapFactory.decodeFile("/sdcard/pictures" + "/" + FileName);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels << 1;
myBitmap = Bitmap.createScaledBitmap(myBitmap,width, height, true);
try {
wallpaperManager.setBitmap( myBitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:7)
将壁纸大小设置为图像大小:
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
wm.setBitmap(bitmap);
wm.suggestDesiredDimensions(w, h);
并记得添加权限:
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
答案 1 :(得分:1)
将宽度高度传递给壁纸管理器,如下所示:
final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(
Context.WALLPAPER_SERVICE);
Bitmap myBitmap = Bitmap.createScaledBitmap(Const.cropped_bitmap, wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), true);
wallpaperManager.suggestDesiredDimensions(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());
try {
wallpaperManager.setBitmap(myBitmap);
Toast.makeText(CropImageActivity.this, CropImageActivity.this.getString(R.string.wallpaper_has_been_set), 0).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(CropImageActivity.this, "Wallpaper not set", 0).show();
}
不要忘记添加权限:
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>