Get color from wallpaper? - Android

蹲街弑〆低调 提交于 2019-12-06 06:46:53

问题


How can I find the (Average) color of the current wallpaper and set that color to a layout on my widget?

An example of what I'm trying to do:

This is a setting on AccuWeather:


回答1:


First you have to get current wallpaper and convert it to bitmap like

final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bitmap = ((BitmapDrawable)wallpaperDrawable).getBitmap();

After that use this link to get dominant color of an image.




回答2:


You can use WallpaperManager.getWallpaperColors that was added in API 27




回答3:


I think you can use this to get the wallpaper and transfer it to Bitmap:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap bm = ((BitmapDrawable) wallpaperDrawable).getBitmap();



回答4:


You can use Bitmap to find the pixels and you can compare the values to find the average pixels.

For ex-

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

There are many ways you can do this..

Please tell more information what do you want to do?

What you can do is-

get the wallpaper using WallpaperMAnager.

Convert it to Bitmap.

Get all the pixels Above techniques might help you.

Calculate the average using pixel arrays.

Using the average make a color and set your wallpaper to the calculated average color.



来源:https://stackoverflow.com/questions/33623095/get-color-from-wallpaper-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!