zoukankan      html  css  js  c++  java
  • Api demo源码学习(13)App/Activity/SetWallpaper

    本节实现了设置桌面壁纸的功能。

    实现一个WallpaperManager实例,调用wallpaperManager.setBitmap()函数设置桌面壁纸。

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

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.wallpaper_2);
            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实现随机的修改image的颜色的功能
            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);
                    imageView.invalidate();
                }
            });

            // 点击此button修改桌面背景
            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();
                    }
                }
            });
        }
    }

    以上即可。

  • 相关阅读:
    Core Data Migration 之拆分Entity
    Core Data Migration 之拆分Entity
    对 Android 开发者有益的 40 条优化建议
    对 Android 开发者有益的 40 条优化建议
    超实用的Java数组技巧攻略分享!
    ls command not found
    苹果建议开发者在iOS 7正式发布之前把应用提交至App Store
    关于android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错
    Linux_系统时间管理
    Python_元组、字典内建方法详解
  • 原文地址:https://www.cnblogs.com/xutao1988/p/2295163.html
Copyright © 2011-2022 走看看