【问题标题】:How to resize my bitmap as 16*16 size or 32*32如何将我的位图大小调整为 16*16 或 32*32
【发布时间】:2013-09-04 12:47:41
【问题描述】:

我想将我的位图图像调整为图标大小。我为此编写了一个函数。但是它缩小了。但是我想让图像的宽度和高度也更小。它在一定的宽度和高度后不会减少.如何将其缩小到图标大小?

这是我的功能..

            private Bitmap getImagemap(File f){
        try {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //The new size we want to scale to
            final int REQUIRED_SIZE=80;

            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                scale*=2;

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

【问题讨论】:

    标签: android bitmap resize reduce


    【解决方案1】:

    这可能有用且容易,

       Bitmap bmp=Bitmap.createScaledBitmap(yourbitmap, 32, 32, false);
    

    【讨论】:

    • 让我看看..我认为它不会工作..但是我可以在我的函数调用之后调用它..好的让我看看
    • 是的..好主意。谢谢。它只有在我的函数调用后才起作用
    • 接受解决您问题的答案,以便用户可以看到哪个是问题的答案或哪个答案解决了这个问题。
    【解决方案2】:

    缩放选项用于缩放。

    对于您要执行的操作,您需要使用Bitmap.createScaledBitmap(参数不言自明)

    【讨论】:

      【解决方案3】:

      试试这个很有魅力

      private String decodeFile(String path,int DESIREDWIDTH,int DESIREDHEIGHT) {
              Bitmap scaledBitmap = null;
      
              try {
                  // Part 1: Decode image
                  Bitmap unscaledBitmap = ScalingUtilities.decodeFile(path, DESIREDWIDTH, DESIREDHEIGHT, ScalingLogic.FIT);
      
                  if (!(unscaledBitmap.getWidth() <= DESIREDWIDTH && unscaledBitmap.getHeight() <= DESIREDHEIGHT)) {
                      // Part 2: Scale image
                      scaledBitmap = ScalingUtilities.createScaledBitmap(unscaledBitmap, DESIREDWIDTH, DESIREDHEIGHT, ScalingLogic.FIT);
                  } else {
                      unscaledBitmap.recycle();
                  }
                  if(scaledBitmap!=null){
                      return  scaledBitmap;
                  }else{
                      return unscaledBitmap;
                  }
              } catch (Throwable e) {
              }
      
      
      
          }
      

      ScalingUtilities.java

      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;
      
      /**
       * Class containing static utility methods for bitmap decoding and scaling
       *
       * @author 
       */
      public class ScalingUtilities {
      
          /**
           * Utility function for decoding an image resource. The decoded bitmap will
           * be optimized for further scaling to the requested destination dimensions
           * and scaling logic.
           *
           * @param res The resources object containing the image data
           * @param resId The resource id of the image data
           * @param dstWidth Width of destination area
           * @param dstHeight Height of destination area
           * @param scalingLogic Logic to use to avoid image stretching
           * @return Decoded bitmap
           */
          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 decodeFile(String path, int dstWidth, int dstHeight,
                  ScalingLogic scalingLogic) {
              Options options = new Options();
              options.inJustDecodeBounds = true;
              BitmapFactory.decodeFile(path, options);
              options.inJustDecodeBounds = false;
              options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
                      dstHeight, scalingLogic);
              Bitmap unscaledBitmap = BitmapFactory.decodeFile(path, options);
      
              return unscaledBitmap;
          }
      
          /**
           * Utility function for creating a scaled version of an existing bitmap
           *
           * @param unscaledBitmap Bitmap to scale
           * @param dstWidth Wanted width of destination bitmap
           * @param dstHeight Wanted height of destination bitmap
           * @param scalingLogic Logic to use to avoid image stretching
           * @return New scaled bitmap object
           */
          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;
          }
      
          /**
           * ScalingLogic defines how scaling should be carried out if source and
           * destination image has different aspect ratio.
           *
           * CROP: Scales the image the minimum amount while making sure that at least
           * one of the two dimensions fit inside the requested destination area.
           * Parts of the source image will be cropped to realize this.
           *
           * FIT: Scales the image the minimum amount while making sure both
           * dimensions fit inside the requested destination area. The resulting
           * destination dimensions might be adjusted to a smaller size than
           * requested.
           */
          public static enum ScalingLogic {
              CROP, FIT
          }
      
          /**
           * Calculate optimal down-sampling factor given the dimensions of a source
           * image, the dimensions of a destination area and a scaling logic.
           *
           * @param srcWidth Width of source image
           * @param srcHeight Height of source image
           * @param dstWidth Width of destination area
           * @param dstHeight Height of destination area
           * @param scalingLogic Logic to use to avoid image stretching
           * @return Optimal down scaling sample size for decoding
           */
          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;
                  }
              }
          }
      
          /**
           * Calculates source rectangle for scaling bitmap
           *
           * @param srcWidth Width of source image
           * @param srcHeight Height of source image
           * @param dstWidth Width of destination area
           * @param dstHeight Height of destination area
           * @param scalingLogic Logic to use to avoid image stretching
           * @return Optimal source rectangle
           */
          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);
              }
          }
      
          /**
           * Calculates destination rectangle for scaling bitmap
           *
           * @param srcWidth Width of source image
           * @param srcHeight Height of source image
           * @param dstWidth Width of destination area
           * @param dstHeight Height of destination area
           * @param scalingLogic Logic to use to avoid image stretching
           * @return Optimal destination rectangle
           */
          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);
              }
          }
      
      }
      

      【讨论】:

      • 未缩放位图已回收。 else 部分即else{ return unscaledBitmap; } 将抛出空指针
      猜你喜欢
      • 1970-01-01
      • 2011-10-25
      • 2013-05-07
      • 1970-01-01
      • 2022-10-25
      • 2014-08-18
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多