灵通163百科在线-实用的手机,证券免费资源网站

您现在的位置:首页 > Android资源 >

Android学习笔记之BitmapFactory.Options实现图片资源的

教程来源:网络整理     点击数:     更新时间:2017-11-09 11:51

/* *这是源码的实现过程,说实话,我也看不懂所有的东西...因为自己也是个小菜鸟... *不过我们可以分析一下... */ public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) { // we don't throw in this case, thus allowing the caller to only check // the cache, and not force the image to be decoded. if (is == null) { return null; } // we need mark/reset to work properly if (!is.markSupported()) { is = new BufferedInputStream(is, DECODE_BUFFER_SIZE); } // so we can call reset() if a given codec gives up after reading up to // this many bytes. FIXME: need to find out from the codecs what this // value should be. is.mark(1024); Bitmap bm; boolean finish = true; /* *我们来看下面这个函数... */ if (is instanceof AssetManager.AssetInputStream) {//这块是一个判断的过程,判断的东西就是,我们这个图片资源到底来自于什么地方,如果满足这个if条件,那么这个图片资源属于drawable文件下的资源... final int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); /* *这里就是设置缩放的一个过程,其中包含一些参数的设置... *比如说缩放的参数,以及目标面积的大小设定... */ if (opts == null || (opts.inScaled && opts.inBitmap == null)) { float scale = 1.0f; int targetDensity = 0; if (opts != null) { final int density = opts.inDensity; targetDensity = opts.inTargetDensity; if (density != 0 && targetDensity != 0) { scale = targetDensity / (float) density; } } /* *下面这个函数的源码,我也看不到了...应该就是一个解码的操作过程... */ bm = nativeDecodeAsset(asset, outPadding, opts, true, scale);
if (bm != null && targetDensity != 0) bm.setDensity(targetDensity); finish = false; } else { /* *这块就是直接解码操作,这个else满足的条件是图片资源满足指定的大小,因此我们不需要传递scale参数了...也就是代表不用进行缩放... */ bm = nativeDecodeAsset(asset, outPadding, opts); } } else { //这里满足的条件想必大家知道,就是图片资源可能来自于其他地方... // pass some temp storage down to the native code. 1024 is made up, // but should be large enough to avoid too many small calls back // into is.read(...) This number is not related to the value passed // to mark(...) above. /* *通过byte流的形式对图片进行获取,这个也很好理解,如果想获取图片资源,那么必须要经过流的形式,对资源数据进行封装...然后获取... *上面之所以不用流的形式,是因为drawable中的资源有特定的获取方式... */ byte [] tempStorage = null; if (opts != null) tempStorage = opts.inTempStorage; if (tempStorage == null) tempStorage = new byte[16 * 1024]; /* *还是同理,设置参数.... */ if (opts == null || (opts.inScaled && opts.inBitmap == null)) { float scale = 1.0f; int targetDensity = 0; if (opts != null) { final int density = opts.inDensity; targetDensity = opts.inTargetDensity; if (density != 0 && targetDensity != 0) { scale = targetDensity / (float) density; } } bm = nativeDecodeStream(is, tempStorage, outPadding, opts, true, scale); if (bm != null && targetDensity != 0) bm.setDensity(targetDensity); finish = false; } else { bm = nativeDecodeStream(is, tempStorage, outPadding, opts); } } if (bm == null && opts != null && opts.inBitmap != null) { throw new IllegalArgumentException("Problem decoding into existing bitmap"); } //这个方法,将会被最终调用..也就是下面的方法... return finish ? finishDecode(bm, outPadding, opts) : bm; } /* *这个方法是当完成解码操作后需要调用的方法... */ private static Bitmap finishDecode(Bitmap bm, Rect outPadding, Options opts) { if (bm == null || opts == null) { return bm; } final int density = opts.inDensity; if (density == 0) { return bm; } bm.setDensity(density); final int targetDensity = opts.inTargetDensity; if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity) { return bm; } byte[] np = bm.getNinePatchChunk(); int[] lb = bm.getLayoutBounds(); final boolean isNinePatch = np != null && NinePatch.isNinePatchChunk(np); /* *这个函数重要部分就是下面,我们可以看到,通过对参数的一些设定..最后会调用 Bitmap.createScaledBitmap方法... */ if (opts.inScaled || isNinePatch) { float scale = targetDensity / (float) density; if (scale != 1.0f) { final Bitmap oldBitmap = bm; bm = Bitmap.createScaledBitmap(oldBitmap, Math.max(1, (int) (bm.getWidth() * scale + 0.5f)), Math.max(1, (int) (bm.getHeight() * scale + 0.5f)), true); if (bm != oldBitmap) oldBitmap.recycle(); if (isNinePatch) { np = nativeScaleNinePatch(np, scale, outPadding); bm.setNinePatchChunk(np); } if (lb != null) { int[] newLb = new int[lb.length]; for (int i=0; i<lb.length; i++) { newLb[i] = (int)((lb[i]*scale)+.5f); } bm.setLayoutBounds(newLb); } } bm.setDensity(targetDensity); } return bm; }

没有相关教程
UC浏览器 最先进实用的手机端浏览器,最大限度节省页面浏览流量,点击下载
老虎地图 绿色纯免费GPS定位省流量的最佳手机地图,点击下载
网秦手机安全系列 手机杀毒防盗,防火墙,构建手机安全,点击进入
塞班精品游戏 最流行最给力的塞班S60免签名名精品游戏合集,点击进入
阿里通 手机上轻松拨打网络电话和收发短信,高音质超省话费,资费标准
爱科手机电话 国内最好的手机网络电话领先品牌,价格低廉,点击查看
愤怒的小鸟2 全民挑战超流行休闲游戏力推新作-愤怒的小鸟2,点击查看
魔力旺旺 淘宝卖家首选的交易工具

资源下载排行
文章阅读排行