画板可以设置背景且在保存的时候也保存背景

来源:互联网 发布:西门子plc伺服编程案例 编辑:程序博客网 时间:2024/05/21 15:39

最近在做画板项目,其中有一个功能是改变画板的背景,例如换成黑色、白色、绿豆色,或是换成小学时候写过的田字格、算术本等背景,然后用户涂鸦后,可以把当前涂鸦的东西保存,作为图片(.png)保存到U盘上。

最开始背景是作为DrawView(自定义的画图View)的background,然后在bitmap上画,保存的时候直接使用bitmap的compress函数:

Bitmap cacheBitmap = null;cacheBitmap = Bitmap.createBitmap(screen_width, screen_height,Bitmap.Config.ARGB_8888);Runnable save = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubLooper.prepare();long bitmapTime = System.currentTimeMillis();String bitmapDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(bitmapTime));String bitmapDir = "/mnt/usb/sda1" + "/" + "Pictures";String bitmapFileName = String.format("Screenshot_%s.png",bitmapDate);File mScreenshotDir = new File(bitmapDir);mScreenshotDir.mkdirs();String bitmapFilePath = String.format("%s/%s", bitmapDir,bitmapFileName);File f = new File(bitmapFilePath);try {FileOutputStream out = new FileOutputStream(f);cacheBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);out.flush();out.close();Toast.makeText(context, "WhiteBoard has saved!",Toast.LENGTH_SHORT).show();Log.i(TAG, "whiteboard has saved!");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(context, "File not found!",Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Looper.loop();}};

但是这样保存成图片的时候,是不会有背景的。因为根本就没背景啥事嘛!!

后来想到一种方法,把背景bitmap,放在画图的bitmap中,然后在保存画图的bitmap的时候就会有背景:

Bitmap cacheBitmap = null;Bitmap bgBitmap = null;bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bg_suansu);screen_width = windowManager.getDefaultDisplay().getWidth();screen_height = windowManager.getDefaultDisplay().getHeight();cacheBitmap = Bitmap.createBitmap(bgBitmap, 0,0,screen_width,screen_height);
Runnable save = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubLooper.prepare();long bitmapTime = System.currentTimeMillis();String bitmapDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(bitmapTime));String bitmapDir = "/mnt/usb/sda1" + "/" + "Pictures";String bitmapFileName = String.format("Screenshot_%s.png",bitmapDate);File mScreenshotDir = new File(bitmapDir);mScreenshotDir.mkdirs();String bitmapFilePath = String.format("%s/%s", bitmapDir,bitmapFileName);File f = new File(bitmapFilePath);try {FileOutputStream out = new FileOutputStream(f);cacheBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);out.flush();out.close();Toast.makeText(context, "WhiteBoard has saved!",Toast.LENGTH_SHORT).show();Log.i(TAG, "whiteboard has saved!");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(context, "File not found!",Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Looper.loop();}};

这样确实可以把背景也保存下来。看似已经解决问题了。但是,这样会导致另外一个问题,那就是,背景不是固定的,背景是可以随时可以选着的,由用户决定。如果使用这种做法的话,每次设置一个背景,那么bgBitmap每次需要重新获得,且cacheBitmap也需要更新,这样在保存的时候才能把最新的背景保存下来。虽然解决了当前的问题,但是对于设置背景来说,其实更麻烦。

也是偶然,想到,是否可以把两个Bitmap合并,这样只要把背景bgBitmap和cacheBitmap合并就可以,而bgBitmap可以在需要保存的时候由函数getBackground转换而来:

public Bitmap getBackgroundBitmap() {BitmapDrawable bg = (BitmapDrawable) getBackground();Bitmap bgBitmap = bg.getBitmap();return bgBitmap;}
合并函数:
public Bitmap mergeBitmap(Bitmap bitmap1, Bitmap bitmap2) {Bitmap bitmap3 = Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(), bitmap1.getConfig());Canvas canvas = new Canvas(bitmap3);canvas.drawBitmap(bitmap1, new Matrix(), null);canvas.drawBitmap(bitmap2, 0, 0, null); // 0、0为bitmap2写入点的x、y坐标return bitmap3;}

然后再把bitmap保存为图片:

Runnable save = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubLooper.prepare();long bitmapTime = System.currentTimeMillis();String bitmapDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date(bitmapTime));String bitmapDir = "/mnt/usb/sda1" + "/" + "Pictures";String bitmapFileName = String.format("Screenshot_%s.png",bitmapDate);File mScreenshotDir = new File(bitmapDir);mScreenshotDir.mkdirs();String bitmapFilePath = String.format("%s/%s", bitmapDir,bitmapFileName);File f = new File(bitmapFilePath);try {FileOutputStream out = new FileOutputStream(f);mergeBitmap(getBackgroundBitmap(), cacheBitmap).compress(Bitmap.CompressFormat.PNG, 90, out);out.flush();out.close();Toast.makeText(context, "WhiteBoard has saved!",Toast.LENGTH_SHORT).show();Log.i(TAG, "whiteboard has saved!");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(context, "File not found!",Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}Looper.loop();}};

这样就完美实现啦!可以简单得到背景Bitmap,而且对于设置背景来说也没影响,

drawView.setBackgroundResource(itemImages[arg2]);


还是要多看多写。

加油~






0 0
原创粉丝点击