Android畫(huà)圖并保存圖片的具體實(shí)現(xiàn)代碼
更新時(shí)間:2013年07月02日 15:02:02 作者:
這篇文章介紹了在Android中畫(huà)圖并保存圖片的實(shí)例,以下是具體的實(shí)現(xiàn)方法,有需要的朋友可以參考一下
Canvas是一個(gè)畫(huà)布,你可以建立一個(gè)空白的畫(huà)布,就直接new一個(gè)Canvas對(duì)象,不需要參數(shù)。
也可以先使用BitmapFactory創(chuàng)建一個(gè)Bitmap對(duì)象,作為新的Canvas對(duì)象的參數(shù),也就是說(shuō)這個(gè)畫(huà)布不是空白的,
如果你想保存圖片的話,最好是Bitmap是一個(gè)新的,而不是從某個(gè)文件中讀入進(jìn)來(lái)的,或者是Drawable對(duì)象。
然后使用Canvas畫(huà)第一張圖上去,在畫(huà)第二張圖上去,最后使用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)開(kāi)始畫(huà)入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫(huà)入水印
//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 );//直接按照矩陣的比例把源文件畫(huà)入進(jìn)行
return resizedBitmap;
}
也可以先使用BitmapFactory創(chuàng)建一個(gè)Bitmap對(duì)象,作為新的Canvas對(duì)象的參數(shù),也就是說(shuō)這個(gè)畫(huà)布不是空白的,
如果你想保存圖片的話,最好是Bitmap是一個(gè)新的,而不是從某個(gè)文件中讀入進(jìn)來(lái)的,或者是Drawable對(duì)象。
然后使用Canvas畫(huà)第一張圖上去,在畫(huà)第二張圖上去,最后使用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)開(kāi)始畫(huà)入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫(huà)入水印
//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 );//直接按照矩陣的比例把源文件畫(huà)入進(jìn)行
return resizedBitmap;
}
您可能感興趣的文章:
- Android實(shí)現(xiàn)簡(jiǎn)單畫(huà)圖畫(huà)板
- Android 自定義view之畫(huà)圖板實(shí)現(xiàn)方法
- Android畫(huà)圖之抗鋸齒paint和Canvas兩種方式實(shí)例
- Android canvas畫(huà)圖操作之切割畫(huà)布實(shí)現(xiàn)方法(clipRect)
- Android簡(jiǎn)單實(shí)現(xiàn)畫(huà)圖功能
- Android編程畫(huà)圖之抗鋸齒解決方法
- Android入門(mén)之畫(huà)圖詳解
- Android?Studio實(shí)現(xiàn)簡(jiǎn)單繪圖板
相關(guān)文章
詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法
本篇文章主要介紹了詳解Android沉浸式實(shí)現(xiàn)兼容解決辦法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11詳解Dagger2在Android開(kāi)發(fā)中的新用法
本篇文章主要介紹了Dagger2在Android開(kāi)發(fā)中的新用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Kotlin協(xié)程之Flow觸發(fā)與消費(fèi)示例解析
Kotlin協(xié)程中,當(dāng)需要消費(fèi)流時(shí),會(huì)調(diào)用collect方法,觸發(fā)流的消費(fèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09Android自定義TipView仿QQ長(zhǎng)按后的提示窗口
這篇文章主要介紹了Android自定義TipView仿QQ長(zhǎng)按后的提示窗口,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Kotlin 協(xié)程與掛起函數(shù)及suspend關(guān)鍵字深入理解
這篇文章主要為大家介紹了Kotlin 協(xié)程與掛起函數(shù)及suspend關(guān)鍵字深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12