如何将颜色转换为位图?

arc*_*tjn 5 notifications android bitmap background-color

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

我试过了

Drawable d = new ColorDrawable(Color.parseColor("#ffffff"));
Bitmap b = ((BitmapDrawable)d).getbitmap();
Run Code Online (Sandbox Code Playgroud)

但上面的代码给出了错误无法将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);
                    }
                }
            }
    );
Run Code Online (Sandbox Code Playgroud)

Ilj*_* KO 15

就在这里.你这样做:

Bitmap bmp=Bitmap.createBitmap(width,height,Config.ARGB_8888);
Canvas canvas=new Canvas(bmp);
canvas.drawColor(colorInt)
Run Code Online (Sandbox Code Playgroud)

在drawColor()里面你也可以使用Color类的方法设置Color,比如Color.argb()或Color.rgb()

这样,您将获得一个宽度,高度和填充指定颜色的位图.

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

最后,您有一个带有绘图的位图