如何将画布转换为位图以便在sdCard中保存

时间:2014-06-04 22:27:38

标签: android android-canvas surfaceview android-bitmap surfaceholder

我使用SurfaceView在屏幕上移动两个位图图片。 我试过这个:

...
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  ...

  canvas.drawBitmap(bitmap,matrix,paint);
  canvas.drawBitmap(bitmap2,matrix,paint);
}
...

如何将画布保存到sdCard中呢?

public saveCanvasIntoSdCard(Canvas canvas)
{

}

1 个答案:

答案 0 :(得分:4)

解决方案我发现:

public Bitmap getBitmap() {
  Bitmap bmOverlay = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), bitmap2.getConfig());
  Canvas canvas = new Canvas(bmOverlay);
  canvas.drawBitmap(bitmap, matrix, null);
  canvas.drawBitmap(bitmap2, 0, 0, null);
  return bmOverlay;
}

public void save(View view){
  String root = Environment.getExternalStorageDirectory().toString();
  File myDir = new File(root + "/dress");    
  myDir.mkdirs();

  String fname = "save.jpg";
  File file = new File (myDir, fname);
  if (file.exists ()) file.delete (); 
  try {
  FileOutputStream out = new FileOutputStream(file);
  getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, out);
  out.flush();
  out.close();

  } catch (Exception e) {
  e.printStackTrace();
  }

}