下面列出了android.app.WallpaperManager#getDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean isLightWallpaper(Context context) {
if (ActivityCompat.checkSelfPermission(context,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
try {
WallpaperManager manager = WallpaperManager.getInstance(context);
if (manager == null) {
return false;
}
Drawable drawable = manager.getDrawable();
if (!(drawable instanceof BitmapDrawable)) {
return false;
}
return DisplayUtils.isLightColor(
DisplayUtils.bitmapToColorInt(((BitmapDrawable) drawable).getBitmap())
);
} catch (Exception ignore) {
return false;
}
}
private void bindWallpaper(boolean checkPermissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkPermissions) {
boolean hasPermission = checkPermissions((requestCode, permission, grantResult) -> {
bindWallpaper(false);
if (textColorValueNow.equals("auto")) {
updateHostView();
}
});
if (!hasPermission) {
return;
}
}
try {
WallpaperManager manager = WallpaperManager.getInstance(this);
if (manager != null) {
Drawable drawable = manager.getDrawable();
if (drawable != null) {
wallpaper.setImageDrawable(drawable);
}
}
} catch (Exception ignore) {
// do nothing.
}
}
public static BitmapDrawable getBackground(Context context) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
float step = 0;
// 计算出屏幕的偏移量
step = (bm.getWidth() - 480) / (7 - 1);
// 截取相应屏幕的Bitmap
DisplayMetrics dm = new DisplayMetrics();
Bitmap pbm = Bitmap.createBitmap(bm, (int) (5 * step), 0,
dm.widthPixels, dm.heightPixels);
return new BitmapDrawable(pbm);
}
public static Drawable getWallpaperDrawableWithSizeDrawable(Context context,
int width, int height, int alpha) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
bm = BitmapUtils.getAdpterBitmap(bm, width, height);
Drawable d = BitmapUtils.Bitmap2Drawable(bm);
bm = BitmapUtils.setAlpha(bm, alpha);
if (bm != null && bm.isRecycled()) {
bm.recycle();
}
return d;
}
/**
* Set activity background to match home screen wallpaper.
*/
protected void initWallpaper(boolean animate)
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (wallpaperManager != null)
{
ImageView background = (ImageView)findViewById(R.id.themegrid_background);
Drawable wallpaper = wallpaperManager.getDrawable();
if (background != null && wallpaper != null)
{
background.setImageDrawable(wallpaper);
background.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 12)
{
if (animate) {
background.animate().alpha(1f).setDuration(WALLPAPER_DELAY);
} else background.setAlpha(1f);
} else if (Build.VERSION.SDK_INT >= 11) {
background.setAlpha(1f);
}
}
}
}
private Bitmap getActiveWallPaper()
{
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bmp = ((BitmapDrawable)wallpaperDrawable).getBitmap();
if(bmp.getWidth()>0) return bmp.copy(bmp.getConfig(),true);
return Bitmap.createBitmap(150,150, Bitmap.Config.ARGB_8888);
}
public static Bitmap getWallpaper(Context context) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
return bm;
// return new BitmapDrawable(bm);
}
public static Drawable getWallpaperDrawable(Context context) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
Drawable d = BitmapUtils.Bitmap2Drawable(bm);
if (bm != null && bm.isRecycled()) {
bm.recycle();
}
return d;
}
public static Bitmap getWallpaperDrawableWithSizeBitmap(Context context,
int width, int height, int alpha) {
WallpaperManager wallpaperManager = WallpaperManager
.getInstance(context);
// 获取当前壁纸
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
// 将Drawable,转成Bitmap
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();
bm = BitmapUtils.getAdpterBitmap(bm, width, height);
bm = BitmapUtils.setAlpha(bm, alpha);
return bm;
}
/**
* @return The {@link Bitmap} for the current wallpaper, or null if none exists.
*/
public static Bitmap getWallpaperBitmap(WallpaperManager wManager, Context mContext) {
if (null == wManager || null == mContext) return null;
final Bitmap wallpaper = getCurrentWallpaperLocked(wManager, mContext);
if (null != wallpaper) return wallpaper;
final Drawable wallpaperD = wManager.getDrawable();
if (null != wallpaperD) {
return drawableToBitmap(wallpaperD);
}
return null;
}
protected void initWallpaper()
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (wallpaperManager != null)
{
ImageView background = (ImageView)findViewById(R.id.preview_background);
Drawable wallpaper = wallpaperManager.getDrawable();
if (background != null && wallpaper != null)
{
background.setImageDrawable(wallpaper);
}
}
}
/**
* @return The {@link Bitmap} for the current wallpaper, or null if none exists.
*/
public static Bitmap getWallpaperBitmap(WallpaperManager wManager, Context mContext) {
if (null == wManager || null == mContext) return null;
final Bitmap wallpaper = getCurrentWallpaperLocked(wManager, mContext);
if (null != wallpaper) return wallpaper;
final Drawable wallpaperD = wManager.getDrawable();
if (null != wallpaperD) {
return drawableToBitmap(wallpaperD);
}
return null;
}
private void refreshWallpaper () {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
mCoverIv.setImageDrawable(wallpaperDrawable);
}