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

后端 未结 4 1373
庸人自扰
庸人自扰 2020-12-20 10:51

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

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 11:46

    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);
    draw(canvas);
    // 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

提交回复
热议问题