4
int x = 10; 
int y = 10; 
int r = 4; 
Paint mPaint = new Paint(); 
mPaint.setColor(0xFF0000); 
Canvas mCanvas = new Canvas(); 
mCanvas.drawCircle(x,y,r,mPaint); 

Is there any way to convert mCanvas to a Drawable? My goal is to generate drawables with a certain shape and color.

Thanks

Johan
  • 35,120
  • 54
  • 178
  • 293

2 Answers2

8

For simple shapes like your circle, I'd think a Shape Drawable would be easier. For more complicated things, just create a Bitmap for your Canvas to use, then create the Canvas and draw into it, then create a Drawable from your Bitmap. Something like:

int x = 10;
int y = 10;
int r = 4;

Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);

Bitmap bitmap = Bitmap.createBitmap(/* read the docs*/);
Canvas mCanvas = new Canvas(bitmap);
mCanvas.drawCircle(x,y,r,mPaint);

BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);

To be perhaps somewhat pedantic (and hopefully increase your understanding), a Canvas just hosts the "draw" calls and draws into a Bitmap that you specify. This means:

  1. Your example code doesn't do much, since you didn't construct your Canvas with a Bitmap or call setBitmap() on it.
  2. You're not converting your Canvas to a Drawable, you're constructing a Drawable from the Bitmap your Canvas calls draw into.
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Shape Drawables sounds good enough for my case. Ive created some xmls, but i dont see them when i try to plot them out. My normal png files works fine though.. – Johan Jul 05 '12 at 22:50
  • @Johan That sounds like an entirely different issue, one that you should attempt to resolve on your own and then ask another question about if necessary. – Darshan Rivka Whittle Jul 05 '12 at 23:04
0

Taken from another post, here is the psuedo code to do this.

Image on canvas to JPEG file

ByteArrayOutputStream baos = new ByteArrayOutputStream()
Bitmap  bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 

// This converts the bitmap to a drawable
BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bitmap);

Alternately, you could use getDrawingCache() as outlined in another answer of that thread.

Community
  • 1
  • 1
ian.shaun.thomas
  • 3,468
  • 25
  • 40
  • Ok, and what would the type of `view` and `fos` be? – Johan Jul 05 '12 at 18:14
  • You do not have to use view, you could just set a width and a height. As for fos that is the OutputStream. You just need a temporary buffer such as: ByteArrayOutputStream baos = new ByteArrayOutputStream(); I have updated my answer to include this change. – ian.shaun.thomas Jul 05 '12 at 18:18
  • Ok, but how do i use `draw()` without a view? – Johan Jul 05 '12 at 18:34
  • How are you drawing currently, you have to have a view involved somehow to do canvas drawing. The most common method is a class that extends view thus letting you just call draw(canvas) instead of view.draw(canvas) – ian.shaun.thomas Jul 05 '12 at 18:46
  • Im currently using the `OverlayItems` class for android google maps example. And theyre using a drawable as marker – Johan Jul 05 '12 at 18:49