android 產生縮圖

來源:互聯網
上載者:User

 

options.inSampleSize = 4;<br />options.inDither = false;<br />options.inPreferredConfig = Bitmap.Config.ARGB_8888;<br />options.inJustDecodeBounds = false;<br />Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.sam,<br />options);<br />Bitmap b2 = extractMiniThumb(b, 90, 130, true);<br />File f = new File("/sdcard/s.jpg");<br />try {<br />BufferedOutputStream w = new BufferedOutputStream(<br />new FileOutputStream(f));<br />b2.compress(Bitmap.CompressFormat.JPEG, 85, w);<br />} catch (FileNotFoundException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}</p><p>public static Bitmap extractMiniThumb(Bitmap source, int width, int height,<br />boolean recycle) {<br />if (source == null) {<br />return null;<br />}<br />float scale;<br />if (source.getWidth() < source.getHeight()) {<br />scale = width / (float) source.getWidth();<br />} else {<br />scale = height / (float) source.getHeight();<br />}<br />Matrix matrix = new Matrix();<br />matrix.setScale(scale, scale);<br />Bitmap miniThumbnail = transform(matrix, source, width, height, true,<br />recycle);<br />return miniThumbnail;<br />}</p><p>public static Bitmap transform(Matrix scaler, Bitmap source,<br />int targetWidth, int targetHeight, boolean scaleUp, boolean recycle) {<br />int deltaX = source.getWidth() - targetWidth;<br />int deltaY = source.getHeight() - targetHeight;<br />if (!scaleUp && (deltaX < 0 || deltaY < 0)) {<br />/*<br /> * In this case the bitmap is smaller, at least in one dimension,<br /> * than the target. Transform it by placing as much of the image as<br /> * possible into the target and leaving the top/bottom or left/right<br /> * (or both) black.<br /> */<br />Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight,<br />Bitmap.Config.ARGB_8888);<br />Canvas c = new Canvas(b2);<br />int deltaXHalf = Math.max(0, deltaX / 2);<br />int deltaYHalf = Math.max(0, deltaY / 2);<br />Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf<br />+ Math.min(targetWidth, source.getWidth()), deltaYHalf<br />+ Math.min(targetHeight, source.getHeight()));<br />int dstX = (targetWidth - src.width()) / 2;<br />int dstY = (targetHeight - src.height()) / 2;<br />Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight<br />- dstY);<br />c.drawBitmap(source, src, dst, null);<br />if (recycle) {<br />source.recycle();<br />}<br />return b2;<br />}<br />float bitmapWidthF = source.getWidth();<br />float bitmapHeightF = source.getHeight();<br />float bitmapAspect = bitmapWidthF / bitmapHeightF;<br />float viewAspect = (float) targetWidth / targetHeight;<br />if (bitmapAspect > viewAspect) {<br />float scale = targetHeight / bitmapHeightF;<br />if (scale < .9F || scale > 1F) {<br />scaler.setScale(scale, scale);<br />} else {<br />scaler = null;<br />}<br />} else {<br />float scale = targetWidth / bitmapWidthF;<br />if (scale < .9F || scale > 1F) {<br />scaler.setScale(scale, scale);<br />} else {<br />scaler = null;<br />}<br />}<br />Bitmap b1;<br />if (scaler != null) {<br />// this is used for minithumb and crop, so we want to filter here.<br />b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source<br />.getHeight(), scaler, true);<br />} else {<br />b1 = source;<br />}<br />if (recycle && b1 != source) {<br />source.recycle();<br />}<br />int dx1 = Math.max(0, b1.getWidth() - targetWidth);<br />int dy1 = Math.max(0, b1.getHeight() - targetHeight);<br />Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth,<br />targetHeight);<br />if (b2 != b1) {<br />if (recycle || b1 != source) {<br />b1.recycle();<br />}<br />}<br />return b2;<br />}<br />} 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.