1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.app; 18 19 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 import com.example.android.apis.R; 22 23 import java.io.IOException; 24 25 import android.app.Activity; 26 import android.app.WallpaperManager; 27 import android.graphics.Color; 28 import android.graphics.PorterDuff; 29 import android.graphics.drawable.Drawable; 30 import android.os.Bundle; 31 import android.view.View; 32 import android.view.WindowManager; 33 import android.view.View.OnClickListener; 34 import android.widget.Button; 35 import android.widget.ImageView; 36 37 /** 38 * <h3>SetWallpaper Activity</h3> 39 * 40 * <p>This demonstrates the how to write an activity that gets the current system wallpaper, 41 * modifies it and sets the modified bitmap as system wallpaper.</p> 42 */ 43 public class SetWallpaperActivity extends Activity { 44 final static private int[] mColors = 45 {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN, 46 Color.YELLOW, Color.WHITE}; 47 48 /** 49 * Initialization of the Activity after it is first created. Must at least 50 * call {@link android.app.Activity#setContentView setContentView()} to 51 * describe what is to be displayed in the screen. 52 */ 53 @Override 54 protected void onCreate(Bundle savedInstanceState) { 55 // Be sure to call the super class. 56 super.onCreate(savedInstanceState); 57 // See res/layout/wallpaper_2.xml for this 58 // view layout definition, which is being set here as 59 // the content of our screen. 60 setContentView(R.layout.wallpaper_2); 61 final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 62 final Drawable wallpaperDrawable = wallpaperManager.getDrawable(); 63 final ImageView imageView = (ImageView) findViewById(R.id.imageview); 64 imageView.setDrawingCacheEnabled(true); 65 imageView.setImageDrawable(wallpaperDrawable); 66 67 Button randomize = (Button) findViewById(R.id.randomize); 68 randomize.setOnClickListener(new OnClickListener() { 69 public void onClick(View view) { 70 int mColor = (int) Math.floor(Math.random() * mColors.length); 71 wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY); 72 imageView.setImageDrawable(wallpaperDrawable); 73 imageView.invalidate(); 74 } 75 }); 76 77 Button setWallpaper = (Button) findViewById(R.id.setwallpaper); 78 setWallpaper.setOnClickListener(new OnClickListener() { 79 public void onClick(View view) { 80 try { 81 wallpaperManager.setBitmap(imageView.getDrawingCache()); 82 finish(); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 } 86 } 87 }); 88 } 89 } 90 91