`
xwangly
  • 浏览: 130735 次
  • 性别: Icon_minigender_1
  • 来自: 鄂州
社区版块
存档分类
最新评论

Android根据指定的尺寸加载Bitmap

阅读更多

因为图片的尺寸千差百异,要想加载不同的图片,又要保证不影响整体布局的美观,我们可以通过计算需要的尺寸来加载图片。

直接上代码:

 

 

    
    public static synchronized Bitmap decodeSampledBitmapFromStream(
            InputStream in, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(in, null, options);
    }

    /**
     * Calculate an inSampleSize for use in a {@link BitmapFactory.Options}
     * object when decoding bitmaps using the decode* methods from
     * {@link BitmapFactory}. This implementation calculates the closest
     * inSampleSize that will result in the final decoded bitmap having a width
     * and height equal to or larger than the requested width and height. This
     * implementation does not ensure a power of 2 is returned for inSampleSize
     * which can be faster when decoding but results in a larger bitmap which
     * isn't as useful for caching purposes.
     * 
     * @param options
     *            An options object with out* params already populated (run
     *            through a decode* method with inJustDecodeBounds==true
     * @param reqWidth
     *            The requested width of the resulting bitmap
     * @param reqHeight
     *            The requested height of the resulting bitmap
     * @return The value to be used for inSampleSize
     */
    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;

        //先根据宽度进行缩小
        while (width / inSampleSize > reqWidth) {
            inSampleSize++;
        }
        //然后根据高度进行缩小
        while (height / inSampleSize > reqHeight) {
            inSampleSize++;
        }
        return inSampleSize;
    }

 该方法通过指定大小来加载图片,并保证加载的图片宽、高均不超过指定的大小。

 

 

来看看inSampleSize的函义:

        /**
         * If set to a value > 1, requests the decoder to subsample the original
         * image, returning a smaller image to save memory. The sample size is
         * the number of pixels in either dimension that correspond to a single
         * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
         * an image that is 1/4 the width/height of the original, and 1/16 the
         * number of pixels. Any value <= 1 is treated the same as 1. Note: the
         * decoder will try to fulfill this request, but the resulting bitmap
         * may have different dimensions that precisely what has been requested.
         * Also, powers of 2 are often faster/easier for the decoder to honor.
         */
        public int inSampleSize;

 当inSampleSize大于1时,解码器会将宽和高分别缩小inSampleSize倍。

 

上述calculateInSampleSize方法正是通过将宽与高进行比对得出的inSampleSize值。

 

 

分享到:
评论
2 楼 飞不过海 2015-01-11  
Mybeautiful 写道
显然有问题,

return BitmapFactory.decodeStream(in, null, options); 
这句中的 in已经被前面读完了。


对的,这样出来的Bitmap是空的
1 楼 Mybeautiful 2015-01-08  
显然有问题,

return BitmapFactory.decodeStream(in, null, options); 
这句中的 in已经被前面读完了。

相关推荐

    Android 使用内置的Camera应用程序捕获图像

    加载并显示一副图像对内存使用情况有显著的影响,Android提供了一个名为BitmapFactory 的实用程序类,该程序提供了一系列的静态方法,允许通过各种来源加载Bitmap图像。针对我们的需求,将从文件加载图像,并在最初...

    浅析KJFrameForAndroid框架如何高效加载Bitmap

    Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。本文主要是从KJFrameForAndroid框架中分析高效加载Bitmap的方法

    BigPictureLoading:Android自定义无压缩加载超清大图

    该类提供的主要构造方法如下:获取该对象后我们可以通过decodeRegion(rect,mOptions)方法传入需要显示的指定区域,就可以得到指定区域的Bitmap。这个方法的第一个参数就是要显示的矩形区域,第二个参数是...

    android中Glide实现加载图片保存至本地并加载回调监听

    Glide 加载图片保存至本地指定路径 /** * Glide 加载图片保存到本地 * * imgUrl 图片地址 * imgName 图片名称 */ Glide.with(context).load(imgUrl).asBitmap().toBytes().into(new SimpleTarget() { @...

    Android使用AsyncTask加载图片的操作流程

    调用指定大小解析Bitmap方法。 因为是弱引用,所以必须判断引用是否被回收。如果异步任务完成前,用户离开Activity或者设置发生改变,ImageView也可能不存在。 class BitmapWorkerTask extends AsyncTask...

    疯狂Android讲义源码

     5.2.3 指定Action、Category调用  系统Activity 204  5.2.4 Data、Type属性与intent-filter  配置 209  5.2.5 Extra属性 211  5.3 使用Intent创建Tab页面 211  5.4 本章小结 212  第6章 Android应用的资源 ...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    6.13 指定时间置换桌面背景——多AlarmManager事件处理 6.14 判断发送短信后的状态——BroadcastReceiver聆听PendingIntent 6.15 后台服务送出广播信息——sendBroadcast与BroadcastReceiver 6.16 开机程序设计——...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    6.13 指定时间置换桌面背景——多AlarmManager事件处理 6.14 判断发送短信后的状态——BroadcastReceiver聆听PendingIntent 6.15 后台服务送出广播信息——sendBroadcast与BroadcastReceiver 6.16 开机程序设计——...

    Picasso简单封装

    另外在这个类中写了一个图形装换的方法的内部类,可以将图片转换为圆形和指定圆角大小的圆角矩形,其他转换可以根据需要进行扩展。 2.图形工具类BitmapUtils 这个类中都是静态方法,提供了一些对Bitmap对象操作的...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    6.13 指定时间置换桌面背景——多AlarmManager事件处理 6.14 判断发送短信后的状态——BroadcastReceiver聆听PendingIntent 6.15 后台服务送出广播信息——sendBroadcast与BroadcastReceiver 6.16 开机程序设计——...

    Google Android SDK开发范例大全的目录

    6.13 指定时间置换桌面背景——多AlarmManager事件处理 6.14 判断发送短信后的状态——BroadcastReceiver聆听PendingIntent 6.15 后台服务送出广播信息——sendBroadcast与BroadcastReceiver 6.16 开机程序设计——...

    疯狂Android讲义.part2

    7.1.2 Bitmap和BitmapFactory 260 7.2 绘图 263 7.2.1 Android绘图基础:Canvas、 Paint等 263 7.2.2 Path类 267 7.2.3 绘制游戏动画 270 7.3 图形特效处理 278 7.3.1 使用Matrix控制变换 278 7.3.2 使用...

    疯狂Android讲义.part1

    7.1.2 Bitmap和BitmapFactory 260 7.2 绘图 263 7.2.1 Android绘图基础:Canvas、 Paint等 263 7.2.2 Path类 267 7.2.3 绘制游戏动画 270 7.3 图形特效处理 278 7.3.1 使用Matrix控制变换 278 7.3.2 使用...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    6.13 指定时间置换桌面背景——多AlarmManager事件处理 6.14 判断发送短信后的状态——BroadcastReceiver聆听PendingIntent 6.15 后台服务送出广播信息——sendBroadcast与BroadcastReceiver 6.16 开机程序设计...

    android BitmapFactory.Options使用方法详解

    BitmapFactory.Options的使用是在加载图片时,就从图片的加载和使用说起  怎样获取图片的大小? 首先我们把这个图片转成Bitmap,然后再利用Bitmap的getWidth()和getHeight()方法就可以取到图片的宽高了。 新问题又...

    android开发实例大全_王东华

    本书以Android应用程序的开发为主题,并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程。本书的讲述由浅入深,实例全面并典型,几乎囊括了所有和Android应用相关的项目。全书分为...

Global site tag (gtag.js) - Google Analytics