Android画图并保存图片的具体实现代码

所属分类: 软件编程 / Android 阅读数: 2063
收藏 0 赞 0 分享
Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。
也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,
如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。

然后使用Canvas画第一张图上去,在画第二张图上去,最后使用Canvas.save(int flag)的方法进行保存,注意save方法里面的参数可以保存单个图层,
如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。

最后所有的信息都会保存在第一个创建的Bitmap中。代码如下:
Java代码
复制代码 代码如下:

/**
    * create the bitmap from a byte array
    *
    * @param src the bitmap object you want proecss
    * @param watermark the water mark above the src
    * @return return a bitmap object ,if paramter's length is 0,return null
    */ 
   private Bitmap createBitmap( Bitmap src, Bitmap watermark ) 
   { 
       String tag = "createBitmap"; 
       Log.d( tag, "create a new bitmap" ); 
       if( src == null ) 
       { 
           return null; 
       } 

       int w = src.getWidth(); 
       int h = src.getHeight(); 
       int ww = watermark.getWidth(); 
       int wh = watermark.getHeight(); 
       //create the new blank bitmap 
       Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//创建一个新的和SRC长度宽度一样的位图 
       Canvas cv = new Canvas( newb ); 
       //draw src into 
       cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src 
       //draw watermark into 
       cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印 
       //save all clip 
       cv.save( Canvas.ALL_SAVE_FLAG );//保存 
       //store 
       cv.restore();//存储 
       return newb; 
   } 

 对图片进行缩小的方法:
Java代码
复制代码 代码如下:

/**
    * lessen the bitmap
    *
    * @param src bitmap
    * @param destWidth the dest bitmap width
    * @param destHeigth
    * @return new bitmap if successful ,oherwise null
    */ 
   private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth ) 
   { 
       String tag = "lessenBitmap"; 
       if( src == null ) 
       { 
           return null; 
       } 
       int w = src.getWidth();//源文件的大小 
       int h = src.getHeight(); 
       // calculate the scale - in this case = 0.4f 
       float scaleWidth = ( ( float ) destWidth ) / w;//宽度缩小比例 
       float scaleHeight = ( ( float ) destHeigth ) / h;//高度缩小比例 
       Log.d( tag, "bitmap width is :" + w ); 
       Log.d( tag, "bitmap height is :" + h ); 
       Log.d( tag, "new width is :" + destWidth ); 
       Log.d( tag, "new height is :" + destHeigth ); 
       Log.d( tag, "scale width is  :" + scaleWidth ); 
       Log.d( tag, "scale height is  :" + scaleHeight ); 
       Matrix m = new Matrix();//矩阵 
       m.postScale( scaleWidth, scaleHeight );//设置矩阵比例 
       Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩阵的比例把源文件画入进行 
       return resizedBitmap; 
   }
更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多