Android内存中的图片

www345 8年前


public static final Bitmap.Config ALPHA_8
public static final Bitmap.Config ARGB_4444
public static final Bitmap.Config ARGB_8888
public static final Bitmap.Config RGB_565

说白了就ALPHA_8就是Alpha由8位组成
ARGB_4444就是由4个4位组成即16位,
ARGB_8888就是由4个8位组成即32位,
RGB_565就是R为5位,G为6位,B为5位共16位

由此可见:
ALPHA_8 代表8位Alpha位图
ARGB_4444 代表16位ARGB位图
ARGB_8888 代表32位ARGB位图
RGB_565 代表8位RGB位图

位图位数越高代表其可以存储的颜色信息越多,当然图像也就越逼真。

 

图片的分辨率是3776 * 2520,每一点又是由ARGB色组成,每个色素占4个Byte,所以这张图片加载到内存中需要消耗的内存为:
3776 * 2520 * 4byte = 38062080byte,会造成内存溢出,那么如何加载大图片呢?

如何加载大分辨率图片

有时候我们确实会需要加载一些大分辨率的图片,但是对于移动设备而言,哪怕加载能成功那么大的内存也是一种浪费(屏幕分辨率限制),所以就需要想办法把图片按照一定比率压缩,使分辨率降低,以至于又不需要耗费很大的堆内存空间,又可以最大的利用设备屏幕的分辨率来显示图片。这里就用到一个 BitmapFactory.Options对象,下面来介绍它。
BitmapFactory.Options为BitmapFactory的一个内部类,它主要用于设定与存储BitmapFactory加载图片的一些信息。下面是Options中需要用到的属性:
inJustDecodeBounds:如果设置为true,将不把图片的像素数组加载到内存中,仅加载一些额外的数据到Options中。
outHeight:图片的高度。
outWidth:图片的宽度。
inSampleSize:如果设置,图片将依据此采样率进行加载,不能设置为小于1的数。例如设置为4,分辨率宽和高将为原来的1/4,这个时候整体所占内存将是原来的1/16。

 

示例代码:

import android.os.Bundle;  import android.os.Environment;  import android.app.Activity;  import android.graphics.Bitmap;  import android.graphics.BitmapFactory;  import android.graphics.BitmapFactory.Options;  import android.view.Menu;  import android.view.View;  import android.view.WindowManager;  import android.widget.Button;  import android.widget.ImageView;   public class MainActivity extends Activity {      private Button btn_loadimage;      private ImageView iv_bigimage;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            btn_loadimage = (Button) findViewById(R.id.btn_loadimage);          iv_bigimage = (ImageView) findViewById(R.id.iv_bigimage);            btn_loadimage.setOnClickListener(new View.OnClickListener() {                @Override              public void onClick(View v) {                  // Bitmap bitmap=BitmapFactory.decodeFile("/sdcard/a.jpg");                  // iv_bigimage.setImageBitmap(bitmap);                    BitmapFactory.Options opts = new Options();                  // 不读取像素数组到内存中,仅读取图片的信息                  opts.inJustDecodeBounds = true;                  BitmapFactory.decodeFile("/sdcard/a.jpg", opts);                  // 从Options中获取图片的分辨率                  int imageHeight = opts.outHeight;                  int imageWidth = opts.outWidth;                    // 获取Android屏幕的服务                  WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);                  // 获取屏幕的分辨率,getHeight()、getWidth已经被废弃掉了                  // 应该使用getSize(),但是这里为了向下兼容所以依然使用它们                  int windowHeight = wm.getDefaultDisplay().getHeight();                  int windowWidth = wm.getDefaultDisplay().getWidth();                    // 计算采样率                  int scaleX = imageWidth / windowWidth;                  int scaleY = imageHeight / windowHeight;                  int scale = 1;                  // 采样率依照最大的方向为准                  if (scaleX > scaleY && scaleY >= 1) {                      scale = scaleX;                  }                  if (scaleX < scaleY && scaleX >= 1) {                      scale = scaleY;                  }                    // false表示读取图片像素数组到内存中,依照设定的采样率                  opts.inJustDecodeBounds = false;                  // 采样率                  opts.inSampleSize = scale;                  Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/a.jpg", opts);                  iv_bigimage.setImageBitmap(bitmap);                }          });      }  }

加载缩略图

1. 使用inJustDecodeBounds,读bitmap的长和宽。
2. 根据bitmap的长款和目标缩略图的长和宽,计算出inSampleSize的大小。
3. 使用inSampleSize,载入一个大一点的缩略图A
4. 使用createScaseBitmap,将缩略图A,生成我们需要的缩略图B。
5. 回收缩略图A。

需要注意的事情

 createScaseBitmap如果原图和目标缩略图大小一致,那么不会生成一个新的bitmap直接返回bitmap,因此,回收的时候,要判断缩略图A是否就是缩略图B,如果说是的话,不要回收。

/**   * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html   */  public class BitmapUtils {      private static int calculateInSampleSize(BitmapFactory.Options options,              int reqWidth, int reqHeight) {          final int height = options.outHeight;          final int width = options.outWidth;          int inSampleSize = 1;          if (height > reqHeight || width > reqWidth) {              final int halfHeight = height / 2;              final int halfWidth = width / 2;              while ((halfHeight / inSampleSize) > reqHeight                      && (halfWidth / inSampleSize) > reqWidth) {                  inSampleSize *= 2;              }          }          return inSampleSize;      }         // 如果是放大图片,filter决定是否平滑,如果是缩小图片,filter无影响      private static Bitmap createScaleBitmap(Bitmap src, int dstWidth,              int dstHeight) {          Bitmap dst = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);          if (src != dst) { // 如果没有缩放,那么不回收              src.recycle(); // 释放Bitmap的native像素数组          }          return dst;      }         // 从Resources中加载图片      public static Bitmap decodeSampledBitmapFromResource(Resources res,              int resId, int reqWidth, int reqHeight) {          final BitmapFactory.Options options = new BitmapFactory.Options();          options.inJustDecodeBounds = true;          BitmapFactory.decodeResource(res, resId, options); // 读取图片长款          options.inSampleSize = calculateInSampleSize(options, reqWidth,                  reqHeight); // 计算inSampleSize          options.inJustDecodeBounds = false;          Bitmap src = BitmapFactory.decodeResource(res, resId, options); // 载入一个稍大的缩略图          return createScaleBitmap(src, reqWidth, reqHeight); // 进一步得到目标大小的缩略图      }         // 从sd卡上加载图片      public static Bitmap decodeSampledBitmapFromFd(String pathName,              int reqWidth, int reqHeight) {          final BitmapFactory.Options options = new BitmapFactory.Options();          options.inJustDecodeBounds = true;          BitmapFactory.decodeFile(pathName, options);          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);          options.inJustDecodeBounds = false;          Bitmap src = BitmapFactory.decodeFile(pathName, options);          return createScaleBitmap(src, reqWidth, reqHeight);      }  }

来自:http://www.cnblogs.com/huozhong/p/4968410.html