android - SurfaceHolder.lockCanvas() 返回 null

标签 android android-canvas surfaceholder

我正在尝试创建一个 Canvas 并使用以下代码在其上绘制位图:

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);

我遇到的问题是我在代码中检查的问题——lockCanvas() 始终返回 null。我或多或少地在学习过程中学习这一点,所以我对 SurfaceView、SurfaceHolder 或 Canvas 的了解不够,无法以某种方式说出我可能做错了什么;有什么建议吗?

最佳答案

来自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.

关于android - SurfaceHolder.lockCanvas() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203437/

相关文章:

ANDROID 微调器不显示最后选择的项目

android - 错误 A/libc : Fatal signal 11 (SIGSEGV) at 0x00000018 (code=1) in SkPath

用于实时图表的 Android Paths

Android 自定义相机 SurfaceView OnClick 在 sdcard 上保存图像?

Android - Surface 无效

android - 初始化首选项

android - Facebook Android - 安装应用程序和参与应用程序 Activity 未提供延迟链接

android - ListView Adapter 图片加载

android - 使 Canvas 透明

android - 是否可以通过缓冲区将摄像机中的视频录制到文件中?