Beispiel #1
0
        Bitmap drawableToBitmap(Drawable drawable)
        {
            Bitmap bitmap = null;

            if (drawable is BitmapDrawable)
            {
                BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
                if (bitmapDrawable.Bitmap != null)
                {
                    return(bitmapDrawable.Bitmap);
                }
            }
            if (drawable is AdaptiveIconDrawable)
            {
                AdaptiveIconDrawable adaptiveIconDrawable = (AdaptiveIconDrawable)drawable;
                if (!(adaptiveIconDrawable.IntrinsicWidth <= 0 || adaptiveIconDrawable.IntrinsicHeight <= 0))
                {
                    //bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
                    //Canvas c = new Canvas(bitmap);
                    var bd = adaptiveIconDrawable.Background;
                    var fd = adaptiveIconDrawable.Foreground;

                    Drawable[] drr = new Drawable[2];
                    drr[0] = bd;
                    drr[1] = fd;

                    LayerDrawable layerDrawable = new LayerDrawable(drr);

                    int width  = layerDrawable.IntrinsicWidth;
                    int height = layerDrawable.IntrinsicHeight;

                    bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888);

                    Canvas c = new Canvas(bitmap);

                    layerDrawable.SetBounds(0, 0, c.Width, c.Height);
                    layerDrawable.Draw(c);

                    return(bitmap);

                    //adaptiveIconDrawable.SetBounds(0, 0, c.Width, c.Height);
                    //adaptiveIconDrawable.Draw(c);

                    return(bitmap);
                }
            }

            if (drawable.IntrinsicWidth <= 0 || drawable.IntrinsicHeight <= 0)
            {
                bitmap = Bitmap.CreateBitmap(1, 1, Bitmap.Config.Argb8888); // Single color bitmap will be created of 1x1 pixel
            }
            else
            {
                bitmap = Bitmap.CreateBitmap(drawable.IntrinsicWidth, drawable.IntrinsicHeight, Bitmap.Config.Argb8888);
            }

            Canvas canvas = new Canvas(bitmap);

            drawable.SetBounds(0, 0, canvas.Width, canvas.Height);
            drawable.Draw(canvas);
            return(bitmap);
        }