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

2016-02-19 08:59 9 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐Android画图并保存图片的具体实现代码,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - 编程语言 】

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

来源:https://www.tulaoshi.com/n/20160219/1589103.html

延伸阅读
代码如下: import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.json.J...
标签: Web开发
前台: upload.htm 代码如下: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" html xmlns="http://www.w3.org/1999/xhtml" head runat="server" titleupload/title link href="upload.css" rel="Stylesheet" / /head body form ul li button id="S...
大致上,我们发现,下拉刷新的列表和一般列表的区别是,当滚动条在顶端的时候,再往下拉动就会把整个列表拉下来,显示出松开刷新的提示。由此可以看出,在构建这个下拉刷新的组件的时候,只用继承ListView,然后重写onTouchEvent就能实现。还有就是要能在xml布局文件中引用,还需要一个参数为Context,AttributeSet的构造函数。 表...
if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#temp')) drop table #temp 临时表 可以创建本地和全局临时表。本地临时表仅在当前会话中可见;全局临时表在所有会话中都可见。 本地临时表的名称前面有一个编号符 (#table_name),而全局临时表的名称前面有两个编号符 (##table_name)。 SQL 语句使用 CREATE TAB...
代码如下: // 隐藏输入法 InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE); // 显示或者隐藏输入法 imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); toggleSoftInput 这个方法可以转换软件输入法在窗体中的显示状态。如果输入法当前是显示状...

经验教程

507

收藏

28
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部