是否可以像使用 Android 自带的应用程序一样设置壁纸?

发布于 2024-12-10 15:02:39 字数 903 浏览 0 评论 0原文

Android 有一种方法可以设置主屏幕壁纸。用户点击“菜单”,然后选择“壁纸”以从系统设置壁纸。生成的壁纸图像在纵向和横向模式下都可以正确缩放。

我做了一个小应用程序,可以更改主屏幕壁纸。它工作正常,但我不知道在将图像设置为壁纸后使其尺寸正确的秘诀是什么。

我对 1280x1084 的 png 图像执行了此操作,并且还对 320x240 的图像尝试了相同的操作,当设置为主屏幕壁纸时,它们都显示相同的尺寸。

我寻找有关如何像他们一样设置壁纸的教程和示例,但找不到如何操作。您能否向我展示一个代码示例,向我展示其中的秘密,以便生成的壁纸能够正确缩放?

我确信必须使用某种壁纸管理器设置,但我不知道该使用哪一种。

提前致谢。

这是我用来设置壁纸的代码:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WallpaperManager myWallpaperManager = WallpaperManager
            .getInstance(getApplicationContext());

    try {
        myWallpaperManager.setResource(R.drawable.kabanight1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

确实, 埃马德

Android has a way to set the home screen wallpaper. The user taps "menu" and then selects "wallpaper" to set a wallpaper from the system. The resulting wallpaper image is properly scaled in both portrait and landscape mode.

I did a small app that allows the home screen wallpaper to be changed. It works fine but I can't find out what the secret is to get the image to be the correct size after it's set as a wallpaper.

I did this with png images that are 1280x1084 and also tried the same thing with images that are 320x240 and they all are shown the same size when set as a home screen wallpaper.

I looked for tutorials and examples on how to set a wallpaper like they do, but couldn't find out how to do it. Can you show me a code sample showing me the secret to this so the resulting wallpaper is scaled correctly?

I'm sure there must be some kind of WallpaperManager setting to use but I don't know which one to use.

Thanks in advance.

Here's the code I'm using to set the wallpaper:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WallpaperManager myWallpaperManager = WallpaperManager
            .getInstance(getApplicationContext());

    try {
        myWallpaperManager.setResource(R.drawable.kabanight1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Truly,
Emad

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

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

发布评论

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

评论(1

似最初 2024-12-17 15:02:39

我只是在看这个。虽然您可以从 Launcher .apk 中的壁纸图像观察实际文件大小,但更好的方法是查询 WallpaperManager 以获得所需的大小(与方向无关)。

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final int fullWidth = wallpaperManager.getDesiredMinimumWidth();
final int fullHeight = wallpaperManager.getDesiredMinimumHeight();

此时,我创建了一个精确所需大小的新位图,并将我自己的位图写入其中。棘手的部分是计算填充和缩放,以及状态栏。但是,一旦您将这个大小合适的图像写入壁纸,它的工作方式就应该与系统壁纸完全相同。

现在的缺点 - 该图像(尺寸大于屏幕)已正确缩放,但您会注意到它的裁剪方式有所不同,具体取决于您的方向。

我认为如果您想根据方向调整图像大小(以最大化图像大小但不裁剪它),您需要动态壁纸。

I was just looking at this. While you can observe actual file sizes from wallpaper images in the Launcher .apk, a better way is to query the WallpaperManager for the desired size (which is independent of orientation).

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final int fullWidth = wallpaperManager.getDesiredMinimumWidth();
final int fullHeight = wallpaperManager.getDesiredMinimumHeight();

At this point, I create a new bitmap of the exact required size and write my own bitmap into it. The tricky part is calculating padding and scaling, as well as accounting for the status bar. However, once you write this properly-sized image as a wallpaper, it should work exactly like a system wallpaper.

Now the downside - that image (with dimensions larger than your screen) is scaled properly, but you'll note that it is cropped differently depending on your orientation.

I think you'd need a live wallpaper if you wanted to resize the image depending on orientation (to maximize the size of the image but not crop it).

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