Android设置壁纸的几种方案

Android设置壁纸有许多方法,主要思路有两种:

1:通过WallpaperManager设置

2:通过系统程序设置

下文将分开说明:

<1>通过WallpaperManager设置

该方法可以直接将图片置为壁纸,对于所有平台的Android系统都使用,但无法裁剪/调整图片。

try {
    WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
                Context.WALLPAPER_SERVICE);

    if (wallpaper != null) {
       wpm.setBitmap(bitmap);
       Log.i("xzy", "wallpaper not null");
    }
} catch (IOException e) {
    Log.e(TAG, "Failed to set wallpaper: " + e);
}

AndroidManifest.xml中需要申明权限:

<uses-permission android:name = "android.permission.SET_WALLPAPER"/>

<2>通过系统程序设置

通过系统应用设置的优点是可以裁剪图片,设置后的壁纸效果最好,操作体验和平台保持一致。并且该方法不许要申明权限

可行的方法有两种

1:调用系统裁剪Activity,通过裁剪Activity设置壁纸:

            Intent intent = new Intent("com.android.camera.CropImage");
            int width = getActivity().getWallpaperDesiredMinimumWidth();
            int height = getActivity().getWallpaperDesiredMinimumHeight();
            intent.putExtra("outputX",         width);
            intent.putExtra("outputY",         height);
            intent.putExtra("aspectX",         width);
            intent.putExtra("aspectY",         height);
            intent.putExtra("scale",           true);
            intent.putExtra("noFaceDetection", true);
            intent.putExtra("setWallpaper",    true);
            intent.putExtra("data", ((BitmapDrawable) wallpaper).getBitmap());
            startActivity(intent);

该方法有一个弊端,com.android.camera.CropImage是一个可选Action,而非标准Action,因此并分所有Android设备都支持该API,许多设备会出现ActivityNotFoundException.

2.调用系统的Intent.ACTION_ATTACH_DATA,该Intent会唤起所有的设置壁纸程序以及设置联系人头像程序,用户可以通过ChooseActivity进行选择:

该Intent是一个标准Intent,因此所有设置都会支持。

                Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.putExtra("mimeType", "image/*");
                Uri uri = Uri.parse(MediaStore.Images.Media
                        .insertImage(getActivity().getContentResolver(),
                                ((BitmapDrawable) wallpaper).getBitmap(), null, null));
                intent.setData(uri);
                startActivityForResult(intent, SET_WALLPAPER);

以上这些方法推荐只用最后一种,原因很简单:体验好,适配成本低。

作者:xzy2046,转载需注明。博客主页:http://blog.csdn.net/xzy2046

时间: 2024-08-29 20:57:36

Android设置壁纸的几种方案的相关文章

Android设置壁纸和创建桌面图标

写了个小Demo,实现了设置壁纸和创建桌面图标的逻辑: 创建壁纸比较简单,将Drawable转为Bitmap,然后直接用setWallpaper就行了: Bitmap bitmap = BitmapFactory.decodeResource(Main.this.getResources(), R.drawable.wallpaper); try { Main.this.setWallpaper(bitmap); } catch (IOException e) { e.printStackTra

Android设置壁纸

APIDemos里面的一段代码,用来设置壁纸. [1].[代码] [Java]代码 跳至 [1] ? 1 2 3 4 5 6 WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); imageView.setDrawingCacheEnabled(true); wallpaperManager.setBitmap(imageView.getDrawingCache()); 需要权限: <uses-permis

ListView设置条目显示四种方案(listView的优化)

Listview是安卓中比不可少的一道风景,但是我用到listView的时候知道ListView容易造成内存的溢出,如果条目很少的话 ,我们一般的是直接使用,但是对于现在大量的ListView的显示,造成内存的溢出会很常见.话不多说了,先上代码 第一种很好理解,但是容易照成内存的溢出. 效果图(都是死代码 不多写了效果图) item的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm

【转】Android——设置颜色的三种方法

以下三种方法均由[http://www.cnblogs.com/six-moon/p/4411383.html]转载. 1.利于系统自带的颜色类.如:TextView1.setTextColor(Android.graphics.Color.RED); 2.数字颜色表示法.如:TextView1.setTextColor(0x000000);//黑色 3.自定义颜色.如:TextView1.setTextColor(this.getResources().getColor(R.drawable.

Android学习笔记进阶21之设置壁纸

别忘记在ApplicationManifest.xml 中加上权限的设置. <uses-permission Android:name = "android.permission.SET_WALLPAPER"/> 壁纸设置方法有三种: 第一 通过WallpaperManager方法中的 setBitmap() 第二 通过WallpaperManager方法中的 setResource() 第三 通过ContextWrapper 类中提供的setWallpaper()方法 由

Android 验证码倒计时两种方案

使用 第一种方案:自定义控件 1.在布局中使用 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_w

android在代码中四种设置控件背景颜色的方法(包括RGB)

转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771  TextView tText=(TextView) findViewById(R.id.textv_name); //第1种: tText.setTextColor(android.graphics.Color.RED);//系统自带的颜色类 // 第2种: tText.setTextColor(0xffff00ff);//0xffff00ff是int类型的数据

android设置动态壁纸 (Wallpaper) 介绍

当进入改壁纸的设置页面 但是还没有设置时 09-21 07:55:05.575: INFO/System.out(1337): service onCreate09-21 07:55:05.614: INFO/System.out(1337): service onCreateEngine09-21 07:55:05.634: INFO/System.out(1337): MyEngine09-21 07:55:05.663: INFO/System.out(1337): onCreate09-

Android Launcher 设置壁纸

版本:1.0 日期:2014.11.25 2014.11.26 版权:©kince 特别推荐:泡在网上的日子 一.概述 一般Launcher都带有壁纸设置的功能,Android提供了设置壁纸的API,在包android.app下面的类WallpaperInfo和WallpaperManager.动态壁纸所在的包是android.service.wallpaper,要区别开.但是要注意,WallpaperInfo是描述动态壁纸的类,从WallpaperManager类的getWallpaperIn