2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.apis.app;
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;
23 import java.io.IOException;
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.BitmapDrawable;
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;
38 * <h3>SetWallpaper Activity</h3>
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>
43 public class SetWallpaperActivity extends Activity {
45 * Initialization of the Activity after it is first created. Must at least
46 * call {@link android.app.Activity#setContentView setContentView()} to
47 * describe what is to be displayed in the screen.
50 protected void onCreate(Bundle savedInstanceState) {
51 // Be sure to call the super class.
52 super.onCreate(savedInstanceState);
53 // See assets/res/layout/wallpaper2.xml for this
54 // view layout definition, which is being set here as
55 // the content of our screen.
56 setContentView(R.layout.wallpaper_2);
57 final WallpaperManager mWallpaperManager = WallpaperManager.getInstance(this);
58 final BitmapDrawable mWallpaperDrawable = (BitmapDrawable) mWallpaperManager.getDrawable();
59 final ImageView imageView = (ImageView) findViewById(R.id.imageview);
60 imageView.setDrawingCacheEnabled(true);
61 imageView.setImageDrawable(mWallpaperDrawable);
63 Button randomize = (Button) findViewById(R.id.randomize);
64 randomize.setOnClickListener(new OnClickListener() {
66 public void onClick(View view) {
67 int mColor = (int) Math.floor(Math.random() * mColors.length);
68 mWallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
69 imageView.setImageDrawable(mWallpaperDrawable);
70 imageView.invalidate();
74 Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
75 setWallpaper.setOnClickListener(new OnClickListener() {
77 public void onClick(View view) {
79 mWallpaperManager.setBitmap(imageView.getDrawingCache());
81 } catch (IOException e) {
88 final private int[] mColors =
89 {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
90 Color.YELLOW, Color.WHITE};