• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.View.OnClickListener;
33 import android.widget.Button;
34 import android.widget.ImageView;
35 
36 /**
37  * <h3>SetWallpaper Activity</h3>
38  *
39  * <p>This demonstrates the how to write an activity that gets the current system wallpaper,
40  * modifies it and sets the modified bitmap as system wallpaper.</p>
41  */
42 public class SetWallpaperActivity extends Activity {
43     final static private int[] mColors =
44             {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
45                     Color.YELLOW, Color.WHITE};
46 
47     /**
48      * Initialization of the Activity after it is first created.  Must at least
49      * call {@link android.app.Activity#setContentView setContentView()} to
50      * describe what is to be displayed in the screen.
51      */
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         // Be sure to call the super class.
55         super.onCreate(savedInstanceState);
56         // See res/layout/wallpaper_2.xml for this
57         // view layout definition, which is being set here as
58         // the content of our screen.
59         setContentView(R.layout.wallpaper_2);
60         final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
61         final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
62         final ImageView imageView = (ImageView) findViewById(R.id.imageview);
63         imageView.setDrawingCacheEnabled(true);
64         imageView.setImageDrawable(wallpaperDrawable);
65 
66         Button randomize = (Button) findViewById(R.id.randomize);
67         randomize.setOnClickListener(new OnClickListener() {
68             public void onClick(View view) {
69                 int mColor = (int) Math.floor(Math.random() * mColors.length);
70                 wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
71                 imageView.setImageDrawable(wallpaperDrawable);
72                 imageView.invalidate();
73             }
74         });
75 
76         Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
77         setWallpaper.setOnClickListener(new OnClickListener() {
78             public void onClick(View view) {
79                 try {
80                     wallpaperManager.setBitmap(imageView.getDrawingCache());
81                     finish();
82                 } catch (IOException e) {
83                     e.printStackTrace();
84                 }
85             }
86         });
87     }
88 }
89 
90