Sunday, March 11, 2018

android - SurfaceHolder.lockCanvas() returns null




I'm trying to create a canvas and draw a bitmap to it using the following code:



Paint paint = new Paint();
InputStream is = assets.open("card_art" + File.separator + "texture.png");
Bitmap bitmap = BitmapFactory.decodeStream(is);
cardFrontBackingImageView = new SurfaceView(Order.getContext()).getHolder().lockCanvas();
if (cardFrontBackingImageView == null)
{
Log.e("Canvas creation", "Canvas is null");
}

cardFrontBackingImageView.drawBitmap(bitmap, null, frame, paint);


The problem that I'm running into is the one that I'm checking for in the code--lockCanvas() is consistently returning null. I'm more or less learning this as I go along, so I don't know enough about the SurfaceView, SurfaceHolder, or Canvas to say one way or another what I might be doing wrong; any suggestions?


Answer



From the doc:




The returned Canvas can be
used to draw into the surface's bitmap. A null is returned if the

surface has not been created or otherwise cannot be edited. You will
usually need to implement Callback.surfaceCreated to find out when the
Surface is available for use.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...