首页 > 解决方案 > C#,Android:创建单色位图

问题描述

我想创建一个只填充一种颜色(#eeeeee)的位图。我设法创建了一个位图并将其设置为它的视图,但我似乎找不到正确的方法来用我想要的颜色完全填充它。输出始终只是一个白色矩形。但它需要像#eeeeee 中那样呈灰色。这是我的努力:

            Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
            Bitmap mutableBitmap = greyBitmap.Copy(Bitmap.Config.Argb8888, true);//<–true makes copy mutable
            Canvas canvas = new Canvas(mutableBitmap);
            Paint paint = new Paint();
            paint.Color = new Color(Color.ParseColor("#eeeeee"));
            canvas.DrawRect(1000, 500, 1000, 500, paint);
            vh.dr = new BitmapDrawable(mutableBitmap);

很有可能,我采取了太多步骤......请随时纠正我。

标签: c#android

解决方案


在 Mike M.

        Bitmap greyBitmap = Bitmap.CreateBitmap(1000, 500, Bitmap.Config.Argb8888);
        Canvas canvas = new Canvas(greyBitmap );
        Paint paint = new Paint();
        paint.Color = new Color(Color.ParseColor("#eeeeee")); // set any color here 
        canvas.DrawRect(0, 0, 1000, 500, paint);
        vh.dr = new BitmapDrawable(greyBitmap );

推荐阅读