国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

Andro id中3種圖片壓縮處理方法

2019-11-02 14:16:32
字體:
來源:轉載
供稿:網友

   這篇文章主要介紹了Android中3種圖片壓縮處理方法,本文講解了質量壓縮方法、獲得縮略圖、圖片縮放三種方法并分別給出示例代碼,需要的朋友可以參考下

  Android中圖片的存在形式:

  1:文件形式:二進制形式存在與硬盤中。

  2:流的形式:二進制形式存在與內存中。

  3:Bitmap的形式

  三種形式的區別:

  文件形式和流的形式:對圖片體積大小并沒有影響。也就是說,如果你手機SD卡上的圖片通過流的形式讀到內存中,在內存中的大小也是原圖的大小。

  注意:不是Bitmap的形式。

  Bitmap的形式:圖片占用的內存會瞬間變大。

  以下是代碼的形式:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 /** * 圖片壓縮的方法總結 */   /* * 圖片壓縮的方法01:質量壓縮方法 */ private Bitmap compressImage(Bitmap beforBitmap) {   // 可以捕獲內存緩沖區的數據,轉換成字節數組。 ByteArrayOutputStream bos = new ByteArrayOutputStream(); if (beforBitmap != null) { // 第一個參數:圖片壓縮的格式;第二個參數:壓縮的比率;第三個參數:壓縮的數據存放到bos中 beforBitmap.compress(CompressFormat.JPEG, 100, bos); int options = 100; // 循環判斷壓縮后的圖片是否是大于100kb,如果大于,就繼續壓縮,否則就不壓縮 while (bos.toByteArray().length / 1024 > 100) { bos.reset();// 置為空 // 壓縮options% beforBitmap.compress(CompressFormat.JPEG, options, bos); // 每次都減少10 options -= 10;   } // 從bos中將數據讀出來 存放到ByteArrayInputStream中 ByteArrayInputStream bis = new ByteArrayInputStream( bos.toByteArray()); // 將數據轉換成圖片 Bitmap afterBitmap = BitmapFactory.decodeStream(bis); return afterBitmap; } return null; }   /* * 圖片壓縮方法02:獲得縮略圖 */ public Bitmap getThumbnail(int id) { // 獲得原圖 Bitmap beforeBitmap = BitmapFactory.decodeResource( mContext.getResources(), id); // 寬 int w = mContext.getResources() .getDimensionPixelOffset(R.dimen.image_w); // 高 int h = mContext.getResources().getDimensionPixelSize(R.dimen.image_h);   // 獲得縮略圖 Bitmap afterBitmap = ThumbnailUtils .extractThumbnail(beforeBitmap, w, h); return afterBitmap;   }   /** * 圖片壓縮03 * * @param id * 要操作的圖片的大小 * @param newWidth * 圖片指定的寬度 * @param newHeight * 圖片指定的高度 * @return */ public Bitmap compressBitmap(int id, double newWidth, double newHeight) { // 獲得原圖 Bitmap beforeBitmap = BitmapFactory.decodeResource( mContext.getResources(), id); // 圖片原有的寬度和高度 float beforeWidth = beforeBitmap.getWidth(); float beforeHeight = beforeBitmap.getHeight();   // 計算寬高縮放率 float scaleWidth = 0; float scaleHeight = 0; if (beforeWidth > beforeHeight) { scaleWidth = ((float) newWidth) / beforeWidth; scaleHeight = ((float)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴东县| 青浦区| 青岛市| 厦门市| 武宁县| 阜宁县| 通道| 阳高县| 张家界市| 屯昌县| 晴隆县| 芮城县| 五河县| 邳州市| 康平县| 普兰店市| 玉林市| 湘阴县| 嘉鱼县| 虎林市| 通州区| 兴国县| 灵川县| 洛南县| 大连市| 达州市| 健康| 方山县| 青田县| 文登市| 陇川县| 巴南区| 祁东县| 保康县| 香格里拉县| 故城县| 获嘉县| 南开区| 饶河县| 沙洋县| 五华县|