缩放位图并设置为壁纸 - 高度变得混乱

时间:2014-06-29 14:16:51

标签: java android bitmap android-bitmap android-wallpaper

我正在尝试将位图缩放到屏幕大小并将其设置为墙纸,但我注意到在某些设备上(屏幕有点宽),壁纸的高度有点凌乱。有人可以帮我按正确的方式缩放图像吗?

这是我的代码:

int widthPx = getWindowManager().getDefaultDisplay()
                        .getWidth();
                int heightPx = getWindowManager().getDefaultDisplay()
                        .getHeight();
                // bmp = Bitmap.createScaledBitmap(bmp, widthPx, heightPx,
                // true);
                BitmapDrawable drawable = (BitmapDrawable) background
                        .getDrawable();

                Bitmap bitmap = drawable.getBitmap();
                try {
                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    Bitmap resizedBitmap = Bitmap.createScaledBitmap(
                            bitmap,
                            myWallpaperManager.getDesiredMinimumWidth(),
                            myWallpaperManager.getDesiredMinimumHeight(),
                            true);
                    myWallpaperManager.setWallpaperOffsetSteps(1, 1);
                    myWallpaperManager.suggestDesiredDimensions(widthPx,
                            heightPx);
                    myWallpaperManager.setBitmap(resizedBitmap);

                    Toast.makeText(BackgroundPreview.this,
                            getString(R.string.wallpaper_set),
                            Toast.LENGTH_SHORT).show();
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(BackgroundPreview.this,
                            getString(R.string.error_setting_wallpaper),
                            Toast.LENGTH_SHORT).show();
                }

0 个答案:

没有答案