Android畫圖並儲存圖片

來源:互聯網
上載者:User
Canvas是一個畫布,你可以建立一個空白的畫布,就直接new一個Canvas對象,不需要參數。也可以先使用BitmapFactory建立一個Bitmap對象,作為新的Canvas對象的參數,也就是說這個畫布不是空白的,如果你想儲存圖片的話,最好是Bitmap是一個新的,而不是從某個檔案中讀入進來的,或者是Drawable對象。 然後使用Canvas畫第一張圖上去,在畫第二張圖上去,最後使用Canvas.save(int flag)的方法進行儲存,注意save方法裡面的參數可以儲存單個圖層,如果是儲存全部圖層的 話使用 save( Canvas.ALL_SAVE_FLAG )。 最後所有的資訊都會儲存在第一個建立的Bitmap中。代碼如下:Java代碼
  1. /** 
  2.     * create the bitmap from a byte array 
  3.     * 
  4.     * @param src the bitmap object you want proecss 
  5.     * @param watermark the water mark above the src 
  6.     * @return return a bitmap object ,if paramter's length is 0,return null 
  7.     */  
  8.    private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
  9.    {  
  10.        String tag = "createBitmap";  
  11.        Log.d( tag, "create a new bitmap" );  
  12.        if( src == null )  
  13.        {  
  14.            return null;  
  15.        }  
  16.   
  17.        int w = src.getWidth();  
  18.        int h = src.getHeight();  
  19.        int ww = watermark.getWidth();  
  20.        int wh = watermark.getHeight();  
  21.        //create the new blank bitmap  
  22.        Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//建立一個新的和SRC長度寬度一樣的位元影像  
  23.        Canvas cv = new Canvas( newb );  
  24.        //draw src into  
  25.        cv.drawBitmap( src, 0, 0, null );//在 0,0座標開始畫入src  
  26.        //draw watermark into  
  27.        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫入浮水印  
  28.        //save all clip  
  29.        cv.save( Canvas.ALL_SAVE_FLAG );//儲存  
  30.        //store  
  31.        cv.restore();//儲存  
  32.        return newb;  
  33.    }  
  對圖片進行縮小的方法:Java代碼
  1. /** 
  2.     * lessen the bitmap 
  3.     * 
  4.     * @param src bitmap 
  5.     * @param destWidth the dest bitmap width 
  6.     * @param destHeigth 
  7.     * @return new bitmap if successful ,oherwise null 
  8.     */  
  9.    private Bitmap lessenBitmap( Bitmap src, int destWidth, int destHeigth )  
  10.    {  
  11.        String tag = "lessenBitmap";  
  12.        if( src == null )  
  13.        {  
  14.            return null;  
  15.        }  
  16.        int w = src.getWidth();//源檔案的大小  
  17.        int h = src.getHeight();  
  18.        // calculate the scale - in this case = 0.4f  
  19.        float scaleWidth = ( ( float ) destWidth ) / w;//寬度縮小比例  
  20.        float scaleHeight = ( ( float ) destHeigth ) / h;//高度縮小比例  
  21.        Log.d( tag, "bitmap width is :" + w );  
  22.        Log.d( tag, "bitmap height is :" + h );  
  23.        Log.d( tag, "new width is :" + destWidth );  
  24.        Log.d( tag, "new height is :" + destHeigth );  
  25.        Log.d( tag, "scale width is  :" + scaleWidth );  
  26.        Log.d( tag, "scale height is  :" + scaleHeight );  
  27.        Matrix m = new Matrix();//矩陣  
  28.        m.postScale( scaleWidth, scaleHeight );//設定矩陣比例  
  29.        Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩陣的比例把源檔案畫入進行  
  30.        return resizedBitmap;  
  31.    } 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.