在做android 開發(fā)的時候,特別是游戲開發(fā),經(jīng)常會用到不同大小的圖片,比如:適配不同屏大小、以及不同的地方需要不同大小的圖片等等。
創(chuàng)新互聯(lián)公司是一家以成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、品牌設(shè)計、軟件運維、營銷推廣、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為成都混凝土泵車等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
這里給出一個將圖片放大、縮小的處理函數(shù):
/**
* bgp_w_picpath 需要處理的圖片
* newWidth 、newHeight 這張圖片處理后的寬高,這里newWidth 和 newHeight 其有效值是>= 0; 當(dāng)其中一個值為0時候,表示按等寬* 高比來放大或是縮小。如果兩個值都為0,則不進行放大縮小。
* recy_the_bitmap 其值是一個boolean 值。其作用是:對bgp_w_picpath 這張圖片處理完成后是否回收其資源,對于手機這樣的移動設(shè)備開發(fā),其資源非常有限,這個是必須的。
**/
public static Bitmap zoomImage(Bitmap bgp_w_picpath, int newWidth, int newHeight, boolean recy_the_bitmap) { // 記得函數(shù)聲明為static
int width;
int height;
Bitmap bitmap_mage = null;
if(bgp_w_picpath != null && !(newWidth==0 && newHeight==0))
{
float scaleWidth = 1.0f;
float scaleHeight = 1.0f;
width = bgp_w_picpath.getWidth();
height = bgp_w_picpath.getHeight();
Matrix matrix = new Matrix();
if(newWidth > 0)
{
scaleWidth = ((float) newWidth) / width;
}
if(newHeight > 0)
{
scaleHeight = ((float) newHeight) / height;
}
if(scaleWidth == 1.0f)
{
scaleWidth = scaleHeight;
}
if(scaleHeight == 1.0f)
{
scaleHeight = scaleWidth;
}
matrix.postScale(scaleWidth, scaleHeight);
bitmap_mage = Bitmap.createBitmap(bgp_w_picpath, 0, 0, width, height,
matrix, true);
}
if(bitmap_mage != null)
{
if(recy_the_bitmap)
{// 回收資源
bgp_w_picpath.recycle();
bgp_w_picpath = null;
}
return bitmap_mage;
}
else
{
return bgp_w_picpath;
}
}
有什么不妥的地方請大家指出,
當(dāng)前題目:android實用的Bitmap處理函數(shù)
URL分享:http://aaarwkj.com/article40/pcoeho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站設(shè)計公司、外貿(mào)建站、微信公眾號、動態(tài)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)