Android画布的保存,android canvas 绘制bitmap并保存到本地

这篇博客介绍了如何在Android中自定义View并重载draw方法来绘制图形,同时详细阐述了如何将绘制的内容保存到Bitmap以及最终存储到本地相册。博主分享了从获取屏幕尺寸、创建Bitmap、在Canvas上绘制,到保存Bitmap为图片的完整流程,包括使用Intent扫描文件系统更新图库。此外,还提供了Bitmap转换为byte数组的方法。
摘要由CSDN通过智能技术生成

自定义一个view,重载draw方法(不是重载ondraw,不然图片保存下来是空白的。)

自定义一个view,重载draw方法(不是重载ondraw,不然图片保存下来是空白的。)在activity里面findviewbyid找到view,用view调用draw方法。

在activity的oncreate里面:

if (mBackgroundBitmap == null) {

mBackgroundBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.RGB_565);

canvas = new Canvas(mBackgroundBitmap);

}

myView.draw(canvas);

自定义的myView(extend View)里面,

@Override

public void draw(Canvas canvas) {

//canvas.drawLine().....

}

保存到本地:

activity里面一些方法,

public Bitmap getBitmap() {

Bitmap whiteBgBitmap = Bitmap.createBitmap(mBackgroundBitmap.getWidth(), mBackgroundBitmap.getHeight(),

Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(whiteBgBitmap);

canvas.drawColor(Color.WHITE);

canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);

return whiteBgBitmap;

}public boolean addGraphToGallery(Bitmap bmp) {

boolean result = false;

try {

File photo = new File(getAlbumStorageDir("cece"), String.format("cece_bitmap_%d.jpg",

System.currentTimeMillis()));

saveBitmapToJPG(bmp, photo);

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

Uri contentUri = Uri.fromFile(photo);

mediaScanIntent.setData(contentUri);

SpiderNetActivity.this.sendBroadcast(mediaScanIntent);

result = true;

} catch (IOException e) {

e.printStackTrace();

}

return result;

}public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException {

Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(newBitmap);

canvas.drawColor(Color.WHITE);

canvas.drawBitmap(bitmap, 0, 0, null);

OutputStream stream = new FileOutputStream(photo);

newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);

stream.close();

}public File getAlbumStorageDir(String albumName) {

// Get the directory for the user's public pictures directory.

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);

if (!file.mkdirs()) {

Log.e("SignaturePad", "Directory not created");

}

return file;

}开始保存:

Bitmap spiderNetBitmap = getBitmap();

if (addGraphToGallery(spiderNetBitmap)) {

Toast.makeText(SpiderNetActivity.this, "Spider graph saved into the Gallery", Toast.LENGTH_SHORT)

.show();

} else {

Toast.makeText(SpiderNetActivity.this, "Unable to store Spider graph", Toast.LENGTH_SHORT).show();

}不要忘记在manifest里面添加存储权限:

bitmap转byte[] 方法:

public byte[] Bitmap2Bytes(Bitmap bm) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

return baos.toByteArray();

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值