麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android畫圖并保存圖片的具體實現代碼

2020-04-11 12:05:21
字體:
來源:轉載
供稿:網友
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; 
   }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级黄色毛片免费 | 久草在线视频网 | 一区在线视频观看 | 成年人观看免费视频 | 最新一级毛片 | 久久成人亚洲 | 成人一级黄色大片 | 大片毛片 | 羞羞的小视频 | 91九色视频在线播放 | 久久国产精品二区 | 亚洲视频观看 | 黄网站在线观 | 毛片大全免费看 | 中文字幕精品在线播放 | 色人阁在线视频 | 久草在线综合 | 国产毛片自拍 | 99激情 | 精品一区二区在线播放 | 九一免费国产 | 99ri在线 | 久久99精品国产99久久6男男 | 一本色道久久99精品综合蜜臀 | 欧美18一19sex性护士农村 | 欧美性视频一区二区 | 久久久久久久亚洲精品 | 欧美a∨一区二区三区久久黄 | chengrenzaixian| 成人h视频在线 | 永久免费av在线 | 国产成人强伦免费视频网站 | 欧美一区黄色 | www.热| 国产成人自拍视频在线观看 | 欧美一级黄色录相 | 日韩一级免费毛片 | 九色新网址 | 日本一级黄色大片 | www.com超碰 | 久久精品亚洲一区二区 |