Specify image url for canvas
I have the following code:
canvas.drawBitmap (mBitma开发者_JAVA百科p, 0, 0,null);
how to I make mBitmap
as PATH or an image from the SD card? so when I save the canvas, it will save the image from that URL?
Thanks a lot for any help! :)
If it is a URL, you will need to download the byte stream and save and then use BitmapFactory to decode it into a Bitmap Object.
An example of this can be found here
The same applies for a file, but you can use BitmapFactory.decodeFile(...)
You can't do it, except you create a custom CustomCanvas
class for your purpose. By any means, you still need to download & cache image before draw
.
Here is a link to a tutorial that does this
Gist :
Enable drawing cache :
setDrawingCacheEnabled(true);
Mapping canvas to bitmap :
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);;
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
commandManager.executeAll(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
Saving the image :
final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + "/myAwesomeDrawing.png"));
nBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
精彩评论