posts - 33, comments - 0, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          Canvas是一個(gè)畫布,你可以建立一個(gè)空白的畫布,就直接new一個(gè)Canvas對(duì)象,不需要參數(shù)。
          也可以先使用BitmapFactory創(chuàng)建一個(gè)Bitmap對(duì)象,作為新的Canvas對(duì)象的參數(shù),也就是說(shuō)這個(gè)畫布不是空白的,
          如果你想保存圖片的話,最好是Bitmap是一個(gè)新的,而不是從某個(gè)文件中讀入進(jìn)來(lái)的,或者是Drawable對(duì)象。
           
          然后使用Canvas畫第一張圖上去,在畫第二張圖上去,最后使用Canvas.save(int flag)的方法進(jìn)行保存,注意save方法里面的參數(shù)可以保存單個(gè)圖層,
          如果是保存全部圖層的 話使用 save( Canvas.ALL_SAVE_FLAG )。
           
          最后所有的信息都會(huì)保存在第一個(gè)創(chuàng)建的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 );//創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖  
          23.        Canvas cv = new Canvas( newb );  
          24.        //draw src into  
          25.        cv.drawBitmap( src, 00null );//在 0,0坐標(biāo)開(kāi)始畫入src  
          26.        //draw watermark into  
          27.        cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角畫入水印  
          28.        //save all clip  
          29.        cv.save( Canvas.ALL_SAVE_FLAG );//保存  
          30.        //store  
          31.        cv.restore();//存儲(chǔ)  
          32.        return newb;  
          33.    }  
           
           對(duì)圖片進(jìn)行縮小的方法:
          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 );//設(shè)置矩陣比例  
          29.        Bitmap resizedBitmap = Bitmap.createBitmap( src, 00, w, h, m, true );//直接按照矩陣的比例把源文件畫入進(jìn)行  
          30.        return resizedBitmap;  
          31.    } 

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 渭南市| 和龙市| 福建省| 平凉市| 卢湾区| 甘孜| 明溪县| 荃湾区| 莆田市| 德阳市| 玉树县| 汉中市| 木里| 多伦县| 乌兰察布市| 恩平市| 类乌齐县| 堆龙德庆县| 靖州| 介休市| 秀山| 南漳县| 安远县| 阿尔山市| 洛南县| 资溪县| 宣武区| 花莲县| 蕲春县| 扎赉特旗| 荆州市| 禹城市| 澎湖县| 佳木斯市| 灌阳县| 定兴县| 许昌县| 宁津县| 建始县| 泾阳县| 卓尼县|