高效的使用Bitmap

在Android經常使用到Bitmap用於顯示圖片,如果圖片過大,容易出現"OutOfMemory"異常,所以要對圖片進行壓縮顯示。

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

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的倍數壓縮效率會高,就是2,4,8…這種,我這裏使用的是更接近需要的壓縮倍數,官方文檔看這裏

使用兩種方式來壓縮圖片,一種是根據需要的圖片長寬,一種是根據需要的圖片大小(就是多少K)。

先看第一種:

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屬性設置爲true,inSampleSize設置完成後再設置爲false。後面的是用來翻轉圖片的。

第二種方式:

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;
	    }
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章