高效的使用Bitmap-創(chuàng)新互聯(lián)

在Android經(jīng)常使用到Bitmap用于顯示圖片,如果圖片過大,容易出現(xiàn)"OutOfMemory"異常,所以要對圖片進(jìn)行壓縮顯示。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比麗江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式麗江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋麗江地區(qū)。費(fèi)用合理售后完善,十年實體公司更值得信賴。

通常使用BitmapFactory類的幾個方法(decodeByteArray(), decodeFile(), decodeResource()等)來建立一個bitmap,在生成bitmap前,可以通過BitmapFactory.Options來設(shè)置屬性,來保證不會出現(xiàn)OutOfMemory異常。首先可以通過需要顯示圖片的長寬來獲取縮小的倍數(shù):

private int calculateInSampleSize(BitmapFactory.Options options,
                int reqWidth, int reqHeight) {
        // Raw height and width of p_w_picpath
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                 inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                 inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
	}

PS:官方文檔說到,圖片壓縮時,使用2的倍數(shù)壓縮效率會高,就是2,4,8…這種,我這里使用的是更接近需要的壓縮倍數(shù),官方文檔看這里。

使用兩種方式來壓縮圖片,一種是根據(jù)需要的圖片長寬,一種是根據(jù)需要的圖片大?。ň褪嵌嗌貹)。

先看第一種:

public Bitmap GetThumbImageByWH(boolean isRound,String imgPath, int p_w_picpathwidth, int p_w_picpathheight) {
		try {
			File picture = new File(imgPath);
			BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
			// set height and width of p_w_picpath
			bitmapFactoryOptions.inJustDecodeBounds = true;
			Bitmap bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
					bitmapFactoryOptions);
			int inSampleSize = calculateInSampleSize(bitmapFactoryOptions,p_w_picpathwidth,p_w_picpathheight);
						
			bitmapFactoryOptions.inSampleSize = inSampleSize;
			bitmapFactoryOptions.inJustDecodeBounds = false;
			bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
					bitmapFactoryOptions);

			return bmap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

PS:如果使用一個BitmapFactory.Options對象,要先把該對象的inJustDecodeBounds屬性設(shè)置為true,inSampleSize設(shè)置完成后再設(shè)置為false。后面的是用來翻轉(zhuǎn)圖片的。

第二種方式:

public Bitmap getThumbImageBySize(String imgpath, int size, boolean adjustOrientation) {
		File file=new File(imgpath);
		FileInputStream fis = null;
		int filesize=0;
		try{
			fis = new FileInputStream(file);
			filesize = fis.available();
			Log.v("file length", ""+filesize);
			fis.close();			
		}catch(Exception ex){
			Log.v("Read file error", ""+ex.getMessage());
		}
		if(filesize/1024<size){
			return BitmapFactory.decodeFile(imgpath);
		}
		// Revision:
		BitmapFactory.Options options = new BitmapFactory.Options();
		// Set it false to not build the bitmap,just record its width and height
		options.inJustDecodeBounds = true;
		// Get the Options object by the path
		BitmapFactory.decodeFile(imgpath, options);
		int height = options.outHeight;
		int width = options.outWidth;
		
		Bitmap smallBitmap = null;
		double multiple = (float)(width*height*4)/(float)(size*1024);
		
		int inSampleSize = (int)Math.ceil(Math.sqrt(((float)filesize/1024.0)/(float)size));
		options.inSampleSize=inSampleSize;
		options.inJustDecodeBounds = false;
		smallBitmap = BitmapFactory.decodeFile(imgpath, options);
		return smallBitmap;
	    }
	}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標(biāo)題:高效的使用Bitmap-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article30/coioso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航、全網(wǎng)營銷推廣品牌網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化

廣告

聲明:本網(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)

外貿(mào)網(wǎng)站建設(shè)