/**     * Calculate an inSampleSize for use in a {@link android.graphics.BitmapFactory.Options} object when decoding     * bitmaps using the decode* methods from {@link android.graphics.BitmapFactory}. This implementation calculates     * the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap     * having a width and height equal to or larger than the requested width and height.     *     * @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) {        // BEGIN_INCLUDE (calculate_sample_size)        // Raw height and width of image        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;            // Calculate the largest inSampleSize value that is a power of 2 and keeps both            // height and width larger than the requested height and width.            while ((halfHeight / inSampleSize) > reqHeight                    && (halfWidth / inSampleSize) > reqWidth) {                inSampleSize *= 2;            }            // This offers some additional logic in case the image has a strange            // aspect ratio. For example, a panorama may have a much larger            // width than height. In these cases the total pixels might still            // end up being too large to fit comfortably in memory, so we should            // be more aggressive with sample down the image (=larger inSampleSize).            long totalPixels = width * height / inSampleSize;            // Anything more than 2x the requested pixels we'll sample down further            final long totalReqPixelsCap = reqWidth * reqHeight * 2;            while (totalPixels > totalReqPixelsCap) {                inSampleSize *= 2;                totalPixels /= 2;            }        }        return inSampleSize;        // END_INCLUDE (calculate_sample_size)    }

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)Volley完全解析(三),定制自
  2. Android(安卓)mutex
  3. Mac系统中,Eclipse没有显示设备
  4. 命令模式与Android(安卓)Handler的工作原
  5. Android(安卓)自动 打包
  6. android 有效加载大Bitmap - 开发文档翻
  7. Android的emulator和ADB工具使用
  8. 【Android入门】Actionbar不显示overflow
  9. 设置Android默认壁纸为动态壁纸
  10. Android(安卓)加载图片并在上面画图总结