Android中擷取和設定手機的壁紙

來源:互聯網
上載者:User
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();                    }                }            });        }    }

 

原文地址:http://topmanopensource.iteye.com/blog/1310542#
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.