问题
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