예제 #1
0
  /**
   * Decodes a bitmap from a file containing it minimizing the memory use, known that the bitmap
   * will be drawn in a surface of reqWidth x reqHeight
   *
   * @param srcPath Absolute path to the file containing the image.
   * @param reqWidth Width of the surface where the Bitmap will be drawn on, in pixels.
   * @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels.
   * @return
   */
  public static Bitmap decodeSampledBitmapFromFile(String srcPath, int reqWidth, int reqHeight) {

    // set desired options that will affect the size of the bitmap
    final Options options = new Options();
    options.inScaled = true;
    options.inPurgeable = true;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
      options.inPreferQualityOverSpeed = false;
    }
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
      options.inMutable = false;
    }

    // make a false load of the bitmap to get its dimensions
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeFile(srcPath, options);

    // calculate factor to subsample the bitmap
    options.inSampleSize = calculateSampleFactor(options, reqWidth, reqHeight);

    // decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(srcPath, options);
  }
예제 #2
0
  /** 压缩图片 */
  public static Bitmap compressImageFromFile(String srcPath) {
    Options newOpts = new Options();
    newOpts.inJustDecodeBounds = true; // 只读边,不读内容
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    float hh = 800f; //
    float ww = 480f; //
    int be = 1;
    if (w > h && w > ww) {
      be = (int) (newOpts.outWidth / ww);
    } else if (w < h && h > hh) {
      be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0) be = 1;
    newOpts.inSampleSize = be; // 设置采样率

    newOpts.inPreferredConfig = Config.ARGB_8888; // 该模式是默认的,可不设
    newOpts.inPurgeable = true; // 同时设置才会有效
    newOpts.inInputShareable = true; // 当系统内存不够时候图片自动被回收

    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    // return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
    // 其实是无效的,大家尽管尝试
    return bitmap;
  }
예제 #3
0
  /**
   * @param data 数据或路径
   * @param width 目标宽
   * @param height 目标高
   * @return 图片
   */
  private Bitmap createBitmap(Object data, int width, int height) {
    Options options = new Options();
    int scale = 1;

    if (width > 0 && height > 0) { // 创建目标大小的图片
      options.inJustDecodeBounds = true;
      if (data instanceof String) {
        BitmapFactory.decodeFile((String) data, options);
      } else {
        BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options);
      }
      int dw = options.outWidth / width;
      int dh = options.outHeight / height;
      scale = Math.max(dw, dh);
      options = new Options();
    }

    options.inDensity = DeviceInfo.getInstance().getDencity();
    options.inScaled = true;
    options.inPurgeable = true;
    options.inSampleSize = scale;

    Bitmap bitmap = null;
    if (data instanceof String) {
      bitmap = BitmapFactory.decodeFile((String) data, options);
    } else {
      bitmap = BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options);
    }
    return bitmap;
  }
  public PurgeableBitmapView(Context context, boolean isPurgeable) {
    super(context);
    setFocusable(true);
    mOptions.inPurgeable = isPurgeable;

    int[] colors = createColors();
    Bitmap src = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
    bitstream = generateBitstream(src, Bitmap.CompressFormat.JPEG, 80);

    mPaint.setTextSize(textSize);
    mPaint.setColor(Color.GRAY);
  }
예제 #5
0
 private void copyOptions(Options srcOptions, Options destOptions) {
   destOptions.inDensity = srcOptions.inDensity;
   destOptions.inDither = srcOptions.inDither;
   destOptions.inInputShareable = srcOptions.inInputShareable;
   destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds;
   destOptions.inPreferredConfig = srcOptions.inPreferredConfig;
   destOptions.inPurgeable = srcOptions.inPurgeable;
   destOptions.inSampleSize = srcOptions.inSampleSize;
   destOptions.inScaled = srcOptions.inScaled;
   destOptions.inScreenDensity = srcOptions.inScreenDensity;
   destOptions.inTargetDensity = srcOptions.inTargetDensity;
   destOptions.inTempStorage = srcOptions.inTempStorage;
   if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions);
   if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions);
 }
예제 #6
0
 public Bitmap loadCachedResource(CachedResource resource) {
   try {
     Options options = new Options();
     options.inPurgeable = true;
     options.inInputShareable = true;
     return BitmapFactory.decodeStream(
         new FlushedInputStream(new FileInputStream(new File(resource.getPath()))));
   } catch (Exception e) {
     if (resource != null) {
       Log.e("MyTag", "Error decoding image : " + resource.getPath(), e);
     }
     System.gc();
     return null;
   }
 }
 public Builder() {
   decodingOptions.inPurgeable = true;
   decodingOptions.inInputShareable = true;
 }