How can I get the bitmap of the canvas I get in onDraw?

draw bitmap on canvas android
draw image on canvas android example
android-canvas draw bitmap from resource
canvas to bitmap android
draw canvas on imageview android
android draw custom shape canvas
android canvas scale

How can I create the bitmap from the canvas of custom view.

There is no way to extract the Bitmap out of a Canvas. The only way you can access it is to pass it yourself when creating the canvas like this new Canvas(myBitmap) and keep the reference.

EDIT2: see @Alex comment blow - the approach of passing a Bitmap to the Canvas does not seem to work for more recent versions of Android.

EDIT : If you don't create the Canvas yourself, you could create a screen-sized Bitmap (or whatever size you need) and then pass it to the Canvas in onDraw calls like this: canvas.setBitmap(myBitmap).

Getting Started with Android Canvas Drawing, Custom drawing can be easy or complex according to your application's needs Override onDraw(); Create Drawing Objects; Handle Layout Events; Draw The Canvas class defines methods for drawing text, lines, bitmaps,� You can also get access to a Canvas object by programatically creating one in code, like this: val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap)

While there is no getBitmap() function for a canvas, since you are making a custom view, what you can do instead is write a function like this inside your view class.

public Bitmap get(){
   return this.getDrawingCache();

This returns the Bitmap of the view, but it is important that in all your constructors you add this,


Otherwise getDrawingCache will return null

Custom Drawing, Rect : The rectangle that the bitmap will be scaled/translated to fit into This value cannot be null . paint, Paint : May be null. The paint used to draw� Because you created a new one. The incoming canvas is a canvas that points to the screen (or more properly, to a buffer in the graphics card that will be drawn to the screen). The one you created draws to a new in-memory bitmap that you created in your create bitmap function, and will draw to that bitmap, not the screen.

I found out that Canvas has a setBitmap function, but not a getBitmap one. It's strange, but anyway, it enables me to create the bitmap myself and pass it to the canvas, retaining the reference.

Canvas, See Paint documentation for a list of attributes that can be set. onDraw(canvas ); // Draw the bitmap that stores the path the user has drawn. // Initially the user� bitmap = BitmapFactory.DecodeResource(Resources,; Now you can draw it as expected: canvas.DrawBitmap(bitmap,100,100, null); I have created a simple example repository on GitHub, which uses your code and draws the bitmap, you can check it out here.

getDrawingCache() is deprecated in API 28.

So now you may use following code inside your custom view

Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// return bitmap; -- this is the bitmap you need

If you want to use this code outside your cusom view use methods like viewInstance.getHeight()... viewInstance.draw(canvas) will draw the view on the bitmap

11.1B: Drawing on a Canvas object � GitBook, Best Java code snippets using (Showing top 20 This should be called when {@link LineChart#onDetachedFromWindow()}. bitmap.recycle(); } } public void onDraw(Canvas c) { //draw onto the canvas if� When looking at the documentation for the Bitmap class, there are a number of createBitmap() methods that take different parameters. These methods may return a mutable or an immutable Bitmap. That is important; only a mutable Bitmap can be used for drawing. On line 27, a Canvas is instantiated based on the Bitmap that you created. java code examples, Since Canvas is a wrapper around a bitmap, you can create a new when you make a custom one you get onDraw() with the Canvas for the� You should initialize all bitmaps in Constructor. Decoding bitmap takes a long time. You can use a HashMap (key, value) to store them. Then in onDraw, get the matched bitmap and draw it directly.

Custom Drawing with Canvas, Draw any shape, view or just bitmaps ? Create something which Get your canvas instance in onDraw and save it for drawing. private Canvas� You need to load your image as bitmap: Resources res = getResources(); Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.your_image); Then make the bitmap mutable and create a canvas over it: Canvas canvas = new Canvas(bitmap.copy(Bitmap.Config.ARGB_8888, true)); You then can draw on the canvas.

Exploring Android Canvas Drawing— For Shapes, Bitmaps , Now that we have the paint setup to have a black color and configured a Let's draw three circles @Override protected void onDraw(Canvas canvas) { canvas. often significantly improve render times by caching the image into a bitmap as� Description I'm getting Crash reports (Devices & Infos below), with the following Exception SKCanvasView.OnDraw (Android.Graphics.Canvas canvas) Java.Lang.OutOfMemoryError: Failed to allocate a 4924812 byte allocation with 1374604 free b

  • I just don't get why it was voted down...
  • @user940016 - your question shows no research, or any attempt to solve the problem yourself. read this for more information:
  • But if I use onDraw, I get a canvas rather than creating it myself, so I don't have a reference to the bitmap...
  • If you don't create the Canvas yourself, you can not interact with its Bitmap. Calling canvas.setBitmap() in onDraw() causes UnsupportedOperationException
  • this may have changed in the last 3 years. The last time I tried was with 2.3 :) What is the version you have tried it with? And thanks for noticing, I will edit accordingly.
  • Exactly.. what I was looking for.Thanks a lot.
  • it will give always null object?
  • @sri - did you add setDrawingCacheEnabled(true) to the constructors of your view?
  • Lifesaver! Thanks a lot. Couldn't find this anywhere.
  • looks like getDrawingCache is being deprecated:…