android實現圖片縮放 旋轉的幾種方法

來源:互聯網
上載者:User

在android應用開發中會出現很多需要實現圖片縮放的地方,或者是出於美觀,或者是出於節省記憶體。。

在這裡博主總結了幾種比較常用的圖片縮放的方法

第一種:通過 BitmapFactory.Options

  BitmapFactory.Options opts = new BitmapFactory.Options();                    opts.inJustDecodeBounds = true;                    BitmapFactory.decodeFile(picpathString, opts);  //只把圖片的邊界資訊載入到記憶體,節省記憶體                    Log.i(TAG, "opts.outHeight=======>" + opts.outHeight);                    Log.i(TAG, "opts.outWidth=====>" + opts.outWidth);                                       if (opts.outHeight != 0 && opts.outWidth != 0)                    {                        opts.inSampleSize = (int) ((opts.outWidth * 5) / width); //inSampleSize這個參數是壓縮的倍數,具體可以自己調節,必須是int                        Log.i(TAG, "opt.inSampleSize---->" + opts.inSampleSize);                    }                  opts.inJustDecodeBounds = false;                    Bitmap bitmap = BitmapFactory.decodeFile(picpathString, opts);  //此時得到的bitmap就是壓縮後的bitmap了

第二種:通過 matrix

// 擷取這個圖片的寬度和高度            int width = _cameraImage.getWidth();            int hegith = _cameraImage.getHeight();            // 建立操作圖片用的Matrix對象            Matrix matrix = new Matrix();            int degree = 90;            Log.i(TAG, "degree--->" + degree);            // 旋轉圖片            matrix.postRotate(degree);            matrix.postScale(NewscaleWidth, NewscaleHeight);//縮放圖片           // 建立新的圖片           Bitmap resizedBitmap = null;           resizedBitmap = Bitmap.createBitmap(_cameraImage, 0, 0, width, hegith, matrix, true);    

第三種:通過canvas對bitmap重繪得到縮放圖

 Rect r = bitmap.getCropRect();//這個方法要自己寫得到原bitmap所佔的矩形地區        int width = r.width(); // CR: final == happy panda!        int height = r.height();             Bitmap newbitmap = null;        newbitmap= Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);{              Canvas canvas = new Canvas(newbitmap);         Rect dstRect = new Rect(0, 0, new_width, new_height); //要把原圖縮放到多大的矩形地區         Paint paint = new Paint();         paint.setAntiAlias(true);         canvas.drawBitmap(newbitmap, r, dstRect, paint); //縮放成功   }

第四種方法:ThumbnailUtils

 Bitmap newbitmap = null;   newbitmap = ThumbnailUtils.extractThumbnail(imageAfterCroped, maxWeight, maxHeight,            OPTIONS_RECYCLE_INPUT);

這是我總結的集中簡單的處理圖片的方法,歡迎交流,轉載請註明出處。謝謝。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.