Android WallpaperManager裁剪图片

时间:2011-09-12 04:35:05

标签: android size screen crop wallpaper

我正在制作一些简单的壁纸应用程序,我有一些图像。它们是可绘制文件夹中的.png文件。

以下是一些代码段:

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); 
....
myWallpaperManager.setResource(R.drawable.image1);

无论我使用什么尺寸或分辨率,设置壁纸时都会裁剪原始图像。当我使用相同的图像作为背景时,它是正确的大小并显示整个图像。我认为这可能是我的模拟器的问题,所以我尝试在实际设备(HTC eris)上运行它,我遇到了同样的问题。我已经将图像设置为eris的屏幕尺寸和分辨率,它仍在裁剪它。然后我使图像只有一英寸高,分辨率为100 dpi。在eris上它是非常像素化的,但仍然裁剪图像。

非常感谢任何帮助。

我试图添加一些图像来显示之前和之后,但作为一个新用户,我被禁止这样做。

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

我遇到了同样的问题。我创建了一个与屏幕大小相同的图像,并为图像添加了填充,使其与WallpaperManager getDesiredMinimumWidth()和getDesiredMinimumHeight()一样大。

让一些代码自动添加填充似乎更好,这就是我在下面使用的。确保图像与屏幕尺寸相同。

private void setWallpaper() {
    try {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        //import non-scaled bitmap wallpaper
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        Bitmap wallpaper = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper, options);

        if (wallpaperManager.getDesiredMinimumWidth() > wallpaper.getWidth() &&
                wallpaperManager.getDesiredMinimumHeight() > wallpaper.getHeight()) {
            //add padding to wallpaper so background image scales correctly
            int xPadding = Math.max(0, wallpaperManager.getDesiredMinimumWidth() - wallpaper.getWidth()) / 2;
            int yPadding = Math.max(0, wallpaperManager.getDesiredMinimumHeight() - wallpaper.getHeight()) / 2;
            Bitmap paddedWallpaper = Bitmap.createBitmap(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight(), Bitmap.Config.ARGB_8888);
            int[] pixels = new int[wallpaper.getWidth() * wallpaper.getHeight()];
            wallpaper.getPixels(pixels, 0, wallpaper.getWidth(), 0, 0, wallpaper.getWidth(), wallpaper.getHeight());
            paddedWallpaper.setPixels(pixels, 0, wallpaper.getWidth(), xPadding, yPadding, wallpaper.getWidth(), wallpaper.getHeight());

            wallpaperManager.setBitmap(paddedWallpaper);
        } else {
            wallpaperManager.setBitmap(wallpaper);
        }
    } catch (IOException e) {
        Log.e(TAG,"failed to set wallpaper");
    }
}

答案 2 :(得分:0)

也许我可以帮忙。


// 1. Get screen size.
DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(metrics);
final int screenWidth  = metrics.widthPixels;
final int screenHeight = metrics.heightPixels;

// 2. Make the wallpaperManager fit the screen size.
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(ViewWallpaperActivity.this);
wallpaperManager.suggestDesiredDimensions(screenWidth, screenHeight);

// 3. Get the desired size.
final int width = wallpaperManager.getDesiredMinimumWidth();
final int height = wallpaperManager.getDesiredMinimumHeight();

 // 4. Scale the wallpaper.
Bitmap bitmap = getBitmap(); // getBitmap(): Get the image to be set as wallpaper.
Bitmap wallpaper = Bitmap.createScaledBitmap(bitmap, width, height, true);

// 5. Set the image as wallpaper.
try {
  wallpaperManager.setBitmap(wallpaper);
} catch (IOException e) {
  e.printStackTrace();
}

请注意,您应该先致电suggestDesiredDimensions,然后分别致电getDesiredMinimumWidthgetDesiredMinimumHeight来获取要缩放的大小。