Android中获取和设置手机的壁纸

package com.easyway.android.ui;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/**
 * 在Android中获取和设置手机的壁纸
 * 
 *  //获取壁纸管理器
 *            final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
 *             //获取壁纸图片
 *            final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
 * 设置壁纸的方法
 * //更换壁纸
           wallpaperManager.setBitmap(imageView.getDrawingCache());
 * @author longgangbai
 *
 */
public class AndroidWallpaperActivity extends Activity {


        final static private int[] mColors =
                {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
                        Color.YELLOW, Color.WHITE};

        /**
         * Initialization of the Activity after it is first created.  Must at least
         * call {@link android.app.Activity#setContentView setContentView()} to
         * describe what is to be displayed in the screen.
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // Be sure to call the super class.
            super.onCreate(savedInstanceState);
            // See res/layout/wallpaper_2.xml for this
            // view layout definition, which is being set here as
            // the content of our screen.
            setContentView(R.layout.wallpaper);
            //获取壁纸管理器
            final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
            //获取壁纸图片
            final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
            //图片视图
            final ImageView imageView = (ImageView) findViewById(R.id.imageview);
            //绘制缓存
            imageView.setDrawingCacheEnabled(true);
            //设置图片
            imageView.setImageDrawable(wallpaperDrawable);
                
            //更改壁纸的颜色
            Button randomize = (Button) findViewById(R.id.randomize);
            randomize.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    int mColor = (int) Math.floor(Math.random() * mColors.length);
                    wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
                    imageView.setImageDrawable(wallpaperDrawable);
                    //调用这个方法用于重新调用onDraw绘制整个界面
                    imageView.invalidate();
                }
            });

            //
            Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
            setWallpaper.setOnClickListener(new OnClickListener() {
                public void onClick(View view) {
                    try {
                    	//更换壁纸
                        wallpaperManager.setBitmap(imageView.getDrawingCache());
                        finish();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

 

你可能感兴趣的:(android)