2013-05-06 49 views
4

我試圖擴展使用options.inSampleSize位圖,但我理解映射是這樣的縮放位%至80%

  • 如果inSampleSize 1,則所產生的圖像是100%

  • 如果inSampleSize 2然後將所得的圖像爲50%

  • 如果inSampleSize 3然後將所得的圖像爲33%

  • 如果inSampleSize 4然後將得到的圖像是25%

  • 如果inSampleSize 5然後將得到的圖像是20%

但我要的是生成的圖像是80%規模。我如何獲得?

回答

-1

我想你可能會誤解inSampleSize的觀點。它旨在節省內存。如果您只是試圖以80%的大小繪製圖像,那麼在將文件讀入內存後,您可以使用矩陣將其縮小(或增大)。

+0

調整大小是否會縮放? – 2013-05-06 03:40:55

+0

如果您使用矩陣縮放圖像,如果您在縮放調用中對兩個參數使用相同的浮點數,它將進行縮放。這裏有一個例子... http://stackoverflow.com/questions/8722359/scale-rotate-bitmap-using-matrix-in-android – csamsonrun 2013-05-06 03:41:48

+0

感謝您與我分享你的知識,也+1。 – 2013-05-06 04:42:33

2

您可以使用此類的calculateSampleSize函數。它純粹是基於計算的。

package com.abhan.example.util; 

import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.Config; 
import android.graphics.BitmapFactory; 
import android.graphics.BitmapFactory.Options; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 

public class ScalingUtilities { 

    public static Bitmap decodeResource(Resources res, int resId, int dstWidth, 
      int dstHeight, ScalingLogic scalingLogic) { 
     Options options = new Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 
     options.inJustDecodeBounds = false; 
     options.inSampleSize = calculateSampleSize(options.outWidth, 
       options.outHeight, dstWidth, dstHeight, scalingLogic); 
     Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, 
       options); 

     return unscaledBitmap; 
    } 

    public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, 
      int dstWidth, int dstHeight, ScalingLogic scalingLogic) { 
     Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), 
       unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic); 
     Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), 
       unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic); 
     Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), 
       dstRect.height(), Config.ARGB_8888); 
     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(
       Paint.FILTER_BITMAP_FLAG)); 

     return scaledBitmap; 
    } 

    public static enum ScalingLogic { 
     CROP, FIT 
    } 

    public static int calculateSampleSize(int srcWidth, int srcHeight, 
      int dstWidth, int dstHeight, ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float) srcWidth/(float) srcHeight; 
      final float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcWidth/dstWidth; 
      } else { 
       return srcHeight/dstHeight; 
      } 
     } else { 
      final float srcAspect = (float) srcWidth/(float) srcHeight; 
      final float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       return srcHeight/dstHeight; 
      } else { 
       return srcWidth/dstWidth; 
      } 
     } 
    } 

    public static Rect calculateSrcRect(int srcWidth, int srcHeight, 
      int dstWidth, int dstHeight, ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.CROP) { 
      final float srcAspect = (float) srcWidth/(float) srcHeight; 
      final float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       final int srcRectWidth = (int) (srcHeight * dstAspect); 
       final int srcRectLeft = (srcWidth - srcRectWidth)/2; 
       return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, 
         srcHeight); 
      } else { 
       final int srcRectHeight = (int) (srcWidth/dstAspect); 
       final int scrRectTop = (int) (srcHeight - srcRectHeight)/2; 
       return new Rect(0, scrRectTop, srcWidth, scrRectTop 
         + srcRectHeight); 
      } 
     } else { 
      return new Rect(0, 0, srcWidth, srcHeight); 
     } 
    } 

    public static Rect calculateDstRect(int srcWidth, int srcHeight, 
      int dstWidth, int dstHeight, ScalingLogic scalingLogic) { 
     if (scalingLogic == ScalingLogic.FIT) { 
      final float srcAspect = (float) srcWidth/(float) srcHeight; 
      final float dstAspect = (float) dstWidth/(float) dstHeight; 

      if (srcAspect > dstAspect) { 
       return new Rect(0, 0, dstWidth, (int) (dstWidth/srcAspect)); 
      } else { 
       return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight); 
      } 
     } else { 
      return new Rect(0, 0, dstWidth, dstHeight); 
     } 
    } 

} 

我希望這可以幫助你。

謝謝。

12

你不能使用Bitmap.createScaledBitmap方法嗎?

int srcWidth = srcBitmap.getWidth(); 
int srcHeight = srcBitmap.getHeight(); 
int dstWidth = (int)(srcWidth*0.8f); 
int dstHeight = (int)(srcHeight*0.8f); 
Bitmap dstBitmap = Bitmap.createScaledBitmap(srcBitmap, dstWidth, dstHeight, true); 
+0

感謝您的幫助。 +1。 – 2013-05-06 04:42:48

+0

這裏的最佳答案:) – CandleCoder 2016-03-04 09:34:00

+0

好的答案,簡單的n短 – 2016-08-02 08:18:32