How to use WallPaper to set wallpaper in Android
This article mainly explains "how to use WallPaper to set wallpaper in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use WallPaper to set up wallpaper in Android.
The specific code is as follows:
Public class SetWallpaperActivity extends Activity {/ / WallpaperManager class: system wallpaper management. Through it, you can get the current wallpaper and set the specified picture as the system wallpaper. Private WallpaperManager wallpaperManager; / / wallpaper corresponding Drawable private Drawable wallpaperDrawable; / / display style ImageView private ImageView imageView; / / randomly generated picture color Button private Button randomize; / / set wallpaper private Button setWallpaper; / / tentative color values final static private int [] mColors = {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.set_wallpaper); / / initialize WallpaperManager wallpaperManager = WallpaperManager.getInstance (this); wallpaperDrawable = wallpaperManager.getDrawable (); / / get the wallpaper initViews () of the current system; initListeners () } private void initListeners () {randomize.setOnClickListener (new OnClickListener () {public void onClick (View view) {int mColor = (int) Math.floor (Math.random () * mColors.length)); / / set the color wallpaperDrawable.setColorFilter (mColors [mColor], PorterDuff.Mode.MULTIPLY) to the current system wallpaper; / / draw intersection imageView.setImageDrawable (wallpaperDrawable) with two layers / / imageView.invalidate ();}}); setWallpaper.setOnClickListener (new OnClickListener () {public void onClick (View view) {try {/ / set wallpaper wallpaperManager.setBitmap (imageView.getDrawingCache ()); finish ();} catch (IOException e) {e.printStackTrace ();}) } private void initViews () {imageView = (ImageView) findViewById (R.id.imageview); imageView.setDrawingCacheEnabled (true); imageView.setImageDrawable (wallpaperDrawable); randomize = (Button) findViewById (R.id.randomize); setWallpaper = (Button) findViewById (R.id.setwallpaper);}}
Layout file:
At this point, I believe you have a deeper understanding of "how to use WallPaper to set up wallpaper in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!