对不起,如果我听起来太菜鸟。有什么方法可以像转换 GradientDrawable
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation orientation, int[] colors);
成位图格式。我正在尝试将 GradientDrawable 设置为设备壁纸。
非常感谢你。:)
对不起,如果我听起来太菜鸟。有什么方法可以像转换 GradientDrawable
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation orientation, int[] colors);
成位图格式。我正在尝试将 GradientDrawable 设置为设备壁纸。
非常感谢你。:)
尝试这个:
这不是一个完美的解决方案,因为我不得不硬编码宽度/高度,GradientDrawable
但无论如何它似乎都可以正常工作。
public static Bitmap drawableToBitmap(Context mContext, int drawableInt) {
Bitmap bitmap;
Drawable drawable = getDrawable(mContext, drawableInt);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}else if(drawable instanceof GradientDrawable){
GradientDrawable gradientDrawable = (GradientDrawable) drawable;
bitmap = Bitmap.createBitmap(140, 140, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
gradientDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
gradientDrawable.draw(canvas);
return bitmap;
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}