Android WallpaperManager 裁剪图像

发布于 2024-12-03 23:33:46 字数 518 浏览 1 评论 0原文

我正在开发一个简单的壁纸应用程序,其中包含我拥有的一些图像。它们是可绘制文件夹中的 .png 文件。

以下是一些代码片段:

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

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

任何帮助将不胜感激。

我尝试添加一些图像来显示之前和之后的情况,但作为新用户,我无法这样做。

I am working on a simple wallpaper app of some images that I have. They are .png files in drawable folders.

Here are some code snippets:

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

No matter what size or resolution I seem to use, when the wallpaper is set it crops the original image. When I use the same image as a background it is the correct size and shows the entire image. I thought it might be a problem with my emulator so I have tried running it on an actual device (HTC eris) and I am having the same problem. I have made the image the screen size and resolution for the eris and it is still cropping it. I then made the image only one inch high and a resolution of 100 dpi. It was very pixelated on the eris, but still cropped the image.

Any help would be greatly appreciated.

I attempted to add some images to show the before and after, but as a newer user I was prevented from doing so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-12-10 23:33:47

也许我可以帮忙。


// 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 来获取要缩放到的尺寸。

Maybe I can help.


// 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();
}

Note that you should call suggestDesiredDimensions, then call getDesiredMinimumWidth and getDesiredMinimumHeight to get the size to be scaled to.

漫雪独思 2024-12-10 23:33:47

我也有同样的问题。我制作了一个与屏幕大小相同的图像,并向图像添加了填充,使其与 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");
    }
}

I had the same problem. I made an image that is the size of the screen and adding padding to the image so that it is as large as the WallpaperManager getDesiredMinimumWidth() and getDesiredMinimumHeight().

It seemed better to have some code automatically add the padding so that is what I used below. Make sure the image is the same size as the screen.

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文