- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要有关 NinePatchDrawable 的帮助:
我的应用可以从网络下载主题。除了 9-Patch PNG 之外,几乎所有东西都可以正常工作。
final Bitmap bubble = getFromTheme("bubble");
if (bubble == null) return null;
final byte[] chunk = bubble.getNinePatchChunk();
if (!NinePatch.isNinePatchChunk(chunk)) return null;
NinePatchDrawable d = new NinePatchDrawable(getResources(), bubble, chunk, new Rect(), null);
v.setBackgroundDrawable(d);
d = null;
System.gc();
getFromTheme() 从 SD 卡加载位图。 9-Patch PNG 已编译,这意味着它们包含所需的 block 。
我将位图转换为 NinePatchDrawable 对象的方式似乎可行,因为图像和我绘制的一样可拉伸(stretch)。
唯一不起作用的是填充。我已经尝试将填充设置为这样的 View :
final Rect rect = new Rect(); // or just use the new Rect() set
d.getPadding(rect); // in the constructor
v.setPadding(rect.left, rect.top, rect.right, rect.bottom);
d.getPadding(rect) 应该用从 block 中获得的填充填充变量 rect,不是吗?但事实并非如此。
结果:TextView (v) 不显示 9-Patch 图像内容区域中的文本。每个坐标中的填充都设置为 0。
感谢阅读。
最佳答案
我终于做到了。 Android 没有正确解释 block 数据。可能有bug。所以你必须自己反序列化 block 来获取填充数据。
开始吧:
package com.dragonwork.example;
import android.graphics.Rect;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
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;
}
}
按如下方式使用上面的类:
final byte[] chunk = bitmap.getNinePatchChunk();
if (NinePatch.isNinePatchChunk(chunk)) {
textView.setBackgroundDrawable(new NinePatchDrawable(getResources(),
bitmap, chunk, NinePatchChunk.deserialize(chunk).mPaddings, null));
}
它会完美地工作!
关于android - NinePatchDrawable 不从 block 中获取填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065996/
我的资源中有九个可绘制的补丁。我必须在运行时更改它的一些颜色,方法是将 NinePatchDrawable 保存为位图并更改像素,然后再次将此位图保存为 NinePatchDrawable。 我的代码
如何从我在/drawable 资源中制作的 NinePatch 图像制作 NinePatchDrawable 对象? 最佳答案 调用 getResources().getDrawable(),提供可绘
我正在尝试设置 Spinner 元素的样式。我为此创建了带有图层列表的资源。我在那里使用标准库资源@drawable/abc_spinner_mtrl_am_alpha。它是一个箭头,它始终显示为白色
我对我的 Android 应用程序有一个要求,即图形上的部分应该是可定制的,方法是从服务器端检索新的颜色和图像。其中一些图像是九 block 图像。 我找不到创建和显示这些 9-patch 图像(已通
我需要有关 NinePatchDrawable 的帮助: 我的应用可以从网络下载主题。除了 9-Patch PNG 之外,几乎所有东西都可以正常工作。 final Bitmap bubble = ge
尝试从资源文件夹加载资源时出现错误: NinePatchDrawable ninePatchObj = ((NinePatchDrawable) context.getResources().getD
我想从 SD 或 Assets 中读取几个 NinePatch 图像,因此没有编译,而且我找不到可以完成这项工作的构造函数/工厂。我也尝试过 NinePatch 工具源代码,但它使用图像的内部结构来完
我正在使用 NinePatchDrawable 创建一个图像,并尝试简单地使用 rotateBy 方法旋转它,但它没有以某种方式旋转。我正在使用以下代码 fragment : TextureAtlas
如何强制 NinePatchDrawable 释放它从“res”中解析出的位图字节? 作为一名 Android 开发者,我面临着控制游戏内存使用的压力。 我努力通过在不再使用资源后尽快释放资源来控制内
我有这行代码: mBgTransition = (TransitionDrawable) mSwitchBg.getBackground(); 背景曾经是一个常规的可绘制对象, 但现在是 9 个补丁
我是一名优秀的程序员,十分优秀!