我如何将颜色转换为位图?
How do i convert to Color to Bitmap?
我有一种整数形式的颜色,我希望该颜色采用位图形式。
有什么办法吗?
我试过了
Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();
但是上面的代码报错Cannot cast ColorDrawable to BitmapDrawable
还有其他方法吗?
实际代码为
Palette.generateAsync(BitmapFactory.decodeFile(songArt),
new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(final Palette palette) {
if (Build.VERSION.SDK_INT >= 16) {
Drawable colorDrawable = new ColorDrawable(palette.getDarkVibrantColor(
getResources().getColor(R.color.noti_background)));
notificationCompat.bigContentView.setImageViewResource(R.id.noti_color_bg,
((BitmapDrawable) colorDrawable).getBitmap());
notificationManager.notify(NOTIFICATION_ID, notificationCompat);
}
}
}
);
是的。
你可以这样做:
Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(colorInt)
在drawColor()
里面你也可以使用Color
class的方法来设置颜色,比如Color.argb(...)
或者Color.rgb(...)
这样您将得到尺寸为 width
/height
并填充指定颜色的位图。
进一步说明:您创建了一个具有指定尺寸的 Bitmap
对象。然后创建一个 Canvas
对象并将 Bitmap
对象附加到它。这样,使用 Canvas
对象方法绘制的所有内容都会绘制在 Bitmap
对象上。
最后你有一个 Bitmap
对象,里面有你的画。
我有一种整数形式的颜色,我希望该颜色采用位图形式。
有什么办法吗?
我试过了
Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();
但是上面的代码报错Cannot cast ColorDrawable to BitmapDrawable
还有其他方法吗?
实际代码为
Palette.generateAsync(BitmapFactory.decodeFile(songArt),
new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(final Palette palette) {
if (Build.VERSION.SDK_INT >= 16) {
Drawable colorDrawable = new ColorDrawable(palette.getDarkVibrantColor(
getResources().getColor(R.color.noti_background)));
notificationCompat.bigContentView.setImageViewResource(R.id.noti_color_bg,
((BitmapDrawable) colorDrawable).getBitmap());
notificationManager.notify(NOTIFICATION_ID, notificationCompat);
}
}
}
);
是的。 你可以这样做:
Bitmap bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(colorInt)
在drawColor()
里面你也可以使用Color
class的方法来设置颜色,比如Color.argb(...)
或者Color.rgb(...)
这样您将得到尺寸为 width
/height
并填充指定颜色的位图。
进一步说明:您创建了一个具有指定尺寸的 Bitmap
对象。然后创建一个 Canvas
对象并将 Bitmap
对象附加到它。这样,使用 Canvas
对象方法绘制的所有内容都会绘制在 Bitmap
对象上。
最后你有一个 Bitmap
对象,里面有你的画。