OSDN Git Service

3875626456ad6f97cc0ab23220a47e354c559863
[android-x86/development.git] / samples / ApiDemos / src / com / example / android / apis / app / SetWallpaperActivity.java
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.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;
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     /**
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.
48      */
49     @Override
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);
62
63         Button randomize = (Button) findViewById(R.id.randomize);
64         randomize.setOnClickListener(new OnClickListener() {
65             @Override
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();
71             }
72         });
73
74         Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
75         setWallpaper.setOnClickListener(new OnClickListener() {
76             @Override
77             public void onClick(View view) {
78                 try {
79                     mWallpaperManager.setBitmap(imageView.getDrawingCache());
80                     finish();
81                 } catch (IOException e) {
82                     e.printStackTrace();
83                 }
84             }
85         });
86     }
87
88     final private int[] mColors =
89             {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
90                     Color.YELLOW, Color.WHITE};
91 }
92