Saving android canvas as a bitmap

*爱你&永不变心* 提交于 2019-12-25 06:27:58

问题


I want to save the canvas object in onDraw() method to be saved as a bitmap. Please do not suggest answers like "view.getDrawingCache(true)" .I want to save canvas directly to a bitmap


回答1:


// first create a mutable bitmap - you determine the size
bkg = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);

// create a canvas with that empty bitmap
Canvas c = new Canvas(bkg);

// do whatever drawing methoods you need....I did a circle
c.drawColor(mContext.getResources().getColor(R.color.off_white));
p.setColor(pixel);
c.drawCircle(width / 2, width / 2, width / 2, p);

// then pull off the entire canvas as a bitmapdrawable (or bitmap, if you perfer)
return new BitmapDrawable(mContext.getResources(), bkg);


来源:https://stackoverflow.com/questions/21294764/saving-android-canvas-as-a-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!