高效加载较大的 Bitmaps

来源:互联网 发布:淘宝客微信采集软件 编辑:程序博客网 时间:2024/04/30 10:25

欢迎大家访问我的博客http://blog.csdn.net/mikejaps专注于android ios  app 开发


今天学习bitmap的加载,下面是代码


</pre><pre class="java" name="code">BitmapFactory.Options options = new BitmapFactory.Options();  //拿到设置参数的optionsoptions.inJustDecodeBounds = true;                             //先设置inJustDecodeBuonds为true,此时不会返回bitmap对象,只会得到宽,高,outMimeTypeBitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;



//就算要压缩的比例
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {    // Raw height and width of image    final int height = options.outHeight;    final int width = options.outWidth;    int inSampleSize = 1;    if (height > reqHeight || width > reqWidth) {        if (width > height) {            inSampleSize = Math.round((float)height / (float)reqHeight);        } else {            inSampleSize = Math.round((float)width / (float)reqWidth);        }    }    return inSampleSize;}public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,        int reqWidth, int reqHeight) {    // First decode with inJustDecodeBounds=true to check dimensions    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeResource(res, resId, options);    // Calculate inSampleSize    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);    // Decode bitmap with inSampleSize set    options.inJustDecodeBounds = false;    return BitmapFactory.decodeResource(res, resId, options);}


有个要注意的 地方: 图片经过处理后的实际大小与前面设定的大小有一点点差别,如设定的尺寸是100*100,但实际 得到的图片大小 可能为 103*103


0 0
原创粉丝点击