How can I create the bitmap from the canvas of custom view.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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.This returns the Bitmap of the view, but it is important that in all your constructors you add this,
Otherwise
getDrawingCache
will returnnull
getDrawingCache() is deprecated in API 28.
So now you may use following code inside your custom view
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
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.
There is no way to extract the
Bitmap
out of aCanvas
. The only way you can access it is to pass it yourself when creating the canvas like thisnew Canvas(myBitmap)
and keep the reference.EDIT2: see @Alex comment blow - the approach of passing a
Bitmap
to theCanvas
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-sizedBitmap
(or whatever size you need) and then pass it to theCanvas
inonDraw
calls like this:canvas.setBitmap(myBitmap)
.