Android 通过inputstream 加载非Drawable 文件夹下的 .9 path 图片
来源:互联网 发布:java 从url去掉ip地址 编辑:程序博客网 时间:2024/06/01 21:13
有时候我们想在其他地方加载 .9 path 图片,比如 assets 目录下,又或者从网上下载一个到sdcard 上,但是 bitmap.getNinePatchChunk() 总是 return null,而如果9path图片在drawable 文件下则可以。为什么会这样呢?是因为9path 图片有两种type,一种叫“source” 一种叫“compiled”. “source” 就是我们通过android sdk 工具制作好的9path 图片,“compiled” 就是打包apk 的时候aapt 会对9path图片进行编译。这就是我们为什么能使用drawable 下的9path图片,所以如果我们想在其他地方也正常使用9path 图片,那么我们就得事先对其进行compile,把编译好的在放在assets 目录或者其他地方。
编译命令 : aapt c -S nine_path_file_floder -C output_floder
// 加载9path并设置background Bitmap bitmap =IconUtils.getBitmapFromFile("assets/easou_ecom_mads_bg_down.9.png"); final byte[] chunk = bitmap.getNinePatchChunk(); if (NinePatch.isNinePatchChunk(chunk)) { bottomLayout.setBackgroundDrawable(new NinePatchDrawable(getContext().getResources(), bitmap, chunk, NinePatchChunk.deserialize(chunk).mPaddings, null)); }//IconUtils.class public static Bitmap getBitmapFromFile(String fileName) { InputStream is = IconUtils.class.getClassLoader().getResourceAsStream(fileName); return BitmapFactory.decodeStream(is); }//NinePatchChunk.classpublic class NinePatchChunk { public static final int NO_COLOR = 0x00000001; public static final int TRANSPARENT_COLOR = 0x00000000; public final Rect mPaddings = new Rect(); public int mDivX[]; public int mDivY[]; public int mColor[]; private static void readIntArray(final int[] data, final ByteBuffer buffer) { for (int i = 0, n = data.length; i < n; ++i) data[i] = buffer.getInt(); } private static void checkDivCount(final int length) { if (length == 0 || (length & 0x01) != 0) throw new RuntimeException("invalid nine-patch: " + length); } public static NinePatchChunk deserialize(final byte[] data) { final ByteBuffer byteBuffer =ByteBuffer.wrap(data).order(ByteOrder.nativeOrder()); if (byteBuffer.get() == 0) return null; // is not serialized final NinePatchChunk chunk = new NinePatchChunk(); chunk.mDivX = new int[byteBuffer.get()]; chunk.mDivY = new int[byteBuffer.get()]; chunk.mColor = new int[byteBuffer.get()]; checkDivCount(chunk.mDivX.length); checkDivCount(chunk.mDivY.length); // skip 8 bytes byteBuffer.getInt(); byteBuffer.getInt(); chunk.mPaddings.left = byteBuffer.getInt(); chunk.mPaddings.right = byteBuffer.getInt(); chunk.mPaddings.top = byteBuffer.getInt(); chunk.mPaddings.bottom = byteBuffer.getInt(); // skip 4 bytes byteBuffer.getInt(); readIntArray(chunk.mDivX, byteBuffer); readIntArray(chunk.mDivY, byteBuffer); readIntArray(chunk.mColor, byteBuffer); return chunk; }}
0 0
- Android 通过inputstream 加载非Drawable 文件夹下的 .9 path 图片
- Android 判断ImageView使用的图片是drawable文件夹下的某个图片
- android工程下drawable文件夹的作用
- Android通过拼接名字获取drawable下的图片(根据图片名称获取在drawable中的ID)
- android如何获取res文件夹下drawable文件夹下的图片资源文件转成一个BitMap对象
- 通过drawable下的图片名获取图片的id
- 帧动画显示drawable文件夹下的指定图片
- 自定义drawable文件夹下的button和图片选择
- drawable文件夹下的图片命名不能为大写!!
- drawable文件夹图片在各个像素密度下的大小
- Android 将drawable下的图片转换成bitmap、Drawable
- drawable下的非图片资源之sharp
- drawable下的非图片资源之selector
- drawable下的非图片资源之layer-list
- android是根据什么去选择drawable文件夹下的三种图片(png,jpg,gif)?图片导入
- Android工具类之图片流InputStream->(byte[])->Bitmap/Drawable
- drawable文件夹下的文件类型
- Android res下三个drawable文件夹的用途
- windows下oracle自动备份
- 【bzoj1059】 ZJOI2007矩阵游戏 二分图匹配
- 内核启动logo
- UITableView使用详解
- 嵌套的多个hover事件处理
- Android 通过inputstream 加载非Drawable 文件夹下的 .9 path 图片
- 软件系统上线前演示剧本
- apt-get fix: remove all packet
- 视音频技术零基础学习笔记(三)及解码程序添加注释
- oracle中 connect by prior 递归算法
- hdu 1268 积木堆砌的城堡(贪心)
- 常见26个jquery使用技巧详解(比如禁止右键点击、隐藏文本框文字等)
- cocos自定义类 绑定到lua
- 消除jquery Mobile自动添加的样式