在 android 上,我正在绘制到 android.graphics.Picture 然后将图片保存到文件中。后来我将图片重新加载到内存中并将其绘制到画布上。我注意到位图从不绘图。经过大量调试后,我设法将问题缩小到 Picture.writeToStream 和 Picture.createFromStream。似乎绘制到图片中的位图没有正确重新加载。下面是我为显示问题而编写的示例代码。在这个示例中,我的画布没有硬件加速。
所以我的问题如下:
- 难道我做错了什么?
- 这是Android错误吗?我提交了错误报告https://code.google.com/p/android/issues/detail?id=54896因为我认为这是。
任何已知的解决方法?
@Override protected void onDraw(Canvas canvas) { try { Picture picture = new Picture(); // Create a bitmap Bitmap bitmap = Bitmap.createBitmap( 100, 100, Config.ARGB_8888); Canvas bitmapCanvas = new Canvas(bitmap); bitmapCanvas.drawARGB(255, 0, 255, 0); // Draw the bitmap to the picture's canvas. Canvas pictureCanvas = picture.beginRecording(canvas.getWidth(), canvas.getHeight()); RectF dstRect = new RectF(0, 0, 200, 200); pictureCanvas.drawBitmap(bitmap, null, dstRect, null); picture.endRecording(); // Save the Picture to a file. File file = File.createTempFile("cache", ".pic"); FileOutputStream os = new FileOutputStream(file); picture.writeToStream(os); os.close(); // Read the picture back in FileInputStream in = new FileInputStream(file); Picture cachedPicture = Picture.createFromStream(in); // Draw the cached picture to the view's canvas. This won't draw the bitmap! canvas.drawPicture(cachedPicture); // Uncomment the following line to see that Drawing the Picture without reloading // it from disk works fine. //canvas.drawPicture(picture); } catch (Exception e) { } }