Android画图并保存图片

Canvas是一个画布,你可以建立一个空白的画布,就直接new一个Canvas对象,不需要参数。

也可以先使用BitmapFactory创建一个Bitmap对象,作为新的Canvas对象的参数,也就是说这个画布不是空白的,

如果你想保存图片的话,最好是Bitmap是一个新的,而不是从某个文件中读入进来的,或者是Drawable对象。


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

如果是保存全部图层的 话使用 save( Canvas.ALL_SAVE_FLAG )。


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

Java代码


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代码

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;
  •     }

图片指定大小显示

1.        Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象

2.        Canvas画布,绘制的目的区域,用于绘图

3.        Bitmap位图,用于图的处理

4.        Matrix矩阵,此例中用于操作图片

二、  步骤

1.        把drawable画到位图对象上

2.        对位图对象做缩放(或旋转等)操作

3.        把位图再转换成drawable

三、示例
[mw_shl_code=java,true]static Bitmap drawableToBitmap(Drawable drawable)// drawable 转换成bitmap
           {
                     int width = drawable.getIntrinsicWidth();   // 取drawable的长宽
                     int height = drawable.getIntrinsicHeight();
                   Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取drawable的颜色格式
                     Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应bitmap
                     Canvas canvas = new Canvas(bitmap);         // 建立对应bitmap的画布
                     drawable.setBounds(0, 0, width, height);
                     drawable.draw(canvas);      // 把drawable内容画到画布中
                     return bitmap;
           }

          static Drawable zoomDrawable(Drawable drawable, int w, int h)
           {
                     int width = drawable.getIntrinsicWidth();
                     int height= drawable.getIntrinsicHeight();
                     Bitmap oldbmp = drawableToBitmap(drawable);// drawable转换成bitmap
                     Matrix matrix = new Matrix();   // 创建操作图片用的Matrix对象
                     float scaleWidth = ((float)w / width);   // 计算缩放比例
                     float scaleHeight = ((float)h / height);
                     matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例
                     Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的bitmap,其内容是对原bitmap的缩放后的图
                     return new BitmapDrawable(newbmp);       // 把bitmap转换成drawable并返回
           }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值