画板可以设置背景且在保存的时候也保存背景
来源:互联网 发布:西门子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
- 画板可以设置背景且在保存的时候也保存背景
- android 画板(选择图片作为背景并保存)
- Android画板开发(四) 添加背景和保存画板内容为图片
- 可以设置背景的JPanel!!
- gvim字体设置、背景设置、保存为默认状态
- gvim字体设置、背景设置、保存为默认状态
- gvim字体设置、背景设置、保存为默认状态
- ListView在拖动的时候背景透明
- 可以设置选项背景颜色的DropDownList
- jquery 设置输入框背景样式透明,按钮背景样式保存不变
- c# 中如何保存透明背景的gif文件
- 关于图片下载后保存背景透明变黑色的问题
- 下载保存的图片背景由透明变为黑色问题解决
- 画板之 android的selector,背景选择器
- MyEclipse编译后,classes文件夹下为空2. 3.删除现在的项目,提前设置好编译文件输出路径,重新导入源文件,设置eclipse为保存时编译,然后在保存的时候就可以自动编译了
- MAP在保存对象时候的优点
- MAP在保存对象时候的优点
- PreferenceActivity,可以保存设置的Activity
- Silverlight DataGrid 绑定的多选、全选
- c++对内存的浅谈以及内存泄漏问题的探讨之四
- jQuery源码解析(架构与依赖模块)笔记二--核心模块
- 控制器瘦身及tableView相关
- ONOS源码笔记--实现
- 画板可以设置背景且在保存的时候也保存背景
- JAVA内部类的作用、方法、使用
- Java多线程中join方法的理解
- hdoj 2005(java) 第几天?
- Kafka+Spark Streaming+Redis实时计算整合实践
- JS二叉树的遍历(前序,中序,后序)
- 解决MySQL读取大数据慢的问题
- ListView的观察者模式的应用,以及调用notifyDataSetChanged()方法时,为什么会刷新ListView
- Multi-class, Multi-label 以及 Multi-task 问题