Set app background to be the same as home screen wallpaper

吃可爱长大的小学妹 提交于 2019-12-10 02:45:00

问题


I want to set my app's background to be the same as my home screen's wallpaper. How can I get the home screen wallpaper in activity.xml? Can I do that?


回答1:


Use

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();

to get the current wallpaper. Then set it as your own app's background:

LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);//Substitute with your layout
ll.setBackground(wallpaperDrawable); 

All this should happen in the onCreate() if you wish to have it as the initial background.




回答2:


While @Robin's answer does set the Activity's background to the home screen's wallpaper, it displays the whole drawable at once, which in most cases distorts it (horizontally compresses it).

A different solution, which may be better or worse depending on your precise needs, is to set your Activity's theme to one that inherits from Theme.Wallpaper. I find this somewhat easier, and the system automatically shows you an appropriate slice of the wallpaper, which is what I want and expect.

res/styles.xml:

<style name="wallpaper_theme" parent="@android:style/Theme.Wallpaper">
</style>

AndroidManifest.xml:

<activity
    ...
    android:theme="@style/wallpaper_theme"
/>


来源:https://stackoverflow.com/questions/24971524/set-app-background-to-be-the-same-as-home-screen-wallpaper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!