Android畫圖并保存圖片的具體實(shí)現(xiàn)代碼
更新時(shí)間:2013年07月02日 15:02:02 作者:
這篇文章介紹了在Android中畫圖并保存圖片的實(shí)例,以下是具體的實(shí)現(xià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代碼
/**
* 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 );//創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐標(biāo)開始畫入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();//存儲(chǔ)
return newb;
}
對(duì)圖片進(jìn)行縮小的方法:
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 );//設(shè)置矩陣比例
Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩陣的比例把源文件畫入進(jìn)行
return resizedBitmap;
}
也可以先使用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代碼
復(fù)制代碼 代碼如下:
/**
* 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 );//創(chuàng)建一個(gè)新的和SRC長(zhǎng)度寬度一樣的位圖
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐標(biāo)開始畫入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();//存儲(chǔ)
return newb;
}
對(duì)圖片進(jìn)行縮小的方法:
Java代碼
復(fù)制代碼 代碼如下:
/**
* 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 );//設(shè)置矩陣比例
Bitmap resizedBitmap = Bitmap.createBitmap( src, 0, 0, w, h, m, true );//直接按照矩陣的比例把源文件畫入進(jìn)行
return resizedBitmap;
}
相關(guān)文章
基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充
本篇文章小編為大家介紹,基于Android中的 AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充。需要的朋友參考下2013-04-04
Android開發(fā)實(shí)現(xiàn)模仿360二維碼掃描功能實(shí)例詳解
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)模仿360二維碼掃描功能,結(jié)合實(shí)例形式詳細(xì)分析了Android開發(fā)二維碼掃描功能所涉及的zxing開源項(xiàng)目文件使用方法及具體掃碼功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
Flutter實(shí)現(xiàn)App功能引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)App功能引導(dǎo)頁(yè),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云
這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解
這篇文章主要為大家介紹了Flutter學(xué)習(xí)之矢量圖SVG的區(qū)域填色示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android控件Tween動(dòng)畫(補(bǔ)間動(dòng)畫)實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android補(bǔ)間動(dòng)畫的原理、功能實(shí)現(xiàn)與布局相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)動(dòng)態(tài)添加標(biāo)簽及其點(diǎn)擊事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

