Bitmap详解之获取Bitmap的方法

Bitmap详解之获取Bitmap的方法

获取Bitmap

1.将文件转化成Bitmap

public static final float DISPLAY_WIDTH = 200;
public static final float DISPLAY_HEIGHT = 200;
/**
 * 从path中获取图片信息
 * @param path
 * @return
 */
private Bitmap decodeBitmap(String path){
  BitmapFactory.Options op = new BitmapFactory.Options();
  //inJustDecodeBounds 
  //If set to true, the decoder will return null (no bitmap), but the out…
  op.inJustDecodeBounds = true;
  Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
  //获取比例大小
  int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
  int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
  //如果超出指定大小,则缩小相应的比例
  if(wRatio > 1 && hRatio > 1){
    if(wRatio > hRatio){
      op.inSampleSize = wRatio;
    }else{
      op.inSampleSize = hRatio;
    }
  }
  op.inJustDecodeBounds = false;
  bmp = BitmapFactory.decodeFile(path, op);
  return bmp;
}

在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。所以用到了我们上面提到的BitmapFactory.Options这个类。

1
public boolean inJustDecodeBounds

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
如果inJustDecodeBounds(boolean flag)的参数是true,则不返回bitmap,但是能获取bitmap的宽和高(op.outWidth和op.outWidth) 。

1
op.inSampleSize = ratio;

设置该属性能够实现图片的压缩

2.Bitmap对象也可以通过Stream二进制流获取

1
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,  bmpFactoryOptions);

3.View转化为Bitmap

//要在子线程内读取
public static Bitmap convertViewToBitmap(View view){
  //当所需要的drawingCache >系统所提供的最大DrawingCache值时,生成Bitmap就会出现问题,此时获取的Bitmap就为null。
  //所以需要修改所需的cache值
  view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  //获取缓存
  view.setDrawingCacheEnabled(true);
  Bitmap bitmap = view.getDrawingCache();
  //清空画图缓冲区
  view.setDrawingCacheEnabled(false);
  return bitmap;
}

或者

public static Bitmap getBitmapFromView(View view)
{
  // Define a bitmap with the same size as the view
  Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
  // Bind a canvas to it
  Canvas canvas = new Canvas(returnedBitmap);
  // Get the view's background
  Drawable bgDrawable = view.getBackground();
  if (bgDrawable != null)
    // has background drawable, then draw it on the canvas
    bgDrawable.draw(canvas);
  else
    // does not have background drawable, then draw white background on
    // the canvas
    canvas.drawColor(Color.WHITE);
  // draw the view on the canvas
  view.draw(canvas);
  // return the bitmap
  return returnedBitmap;
}

或者

//图片不被压缩
public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){
    Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    view.draw(new Canvas(bitmap));

    return bitmap;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值