如何将颜色转换为位图?

时间:2015-07-05 16:47:28

标签: android bitmap notifications background-color

我有一个整数形式的颜色,我希望那个颜色是Bitmap形式 有没有办法这样做?

我试过

Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();

但是上面的代码给出了错误无法将ColorDrawable强制转换为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);
                    }
                }
            }
    );

1 个答案:

答案 0 :(得分:14)

是的。 你只需要这样做:

Bitmap bmp=Bitmap.createBitmap(width,height,Config.ARGB_8888);
Canvas canvas=new Canvas(bmp);
canvas.drawColor(colorInt)

在drawColor()内部,您还可以使用Color类的方法设置Color,如Color.argb()或Color.rgb()

这样你就会有一个宽度,高度的位图,并用指定的颜色填充。

进一步说明:使用指定的尺寸创建位图。然后创建一个画布并将位图附加到它。这样,使用Canvas方法绘制的所有内容都会在Bitmap上绘制。

最后你有一个带有图纸的位图

相关问题