创建一个九补丁图像code [英] Create a nine patch image with code
问题描述
我想以编程方式创建9片图像。我曾尝试<一href="http://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime/14061128#14061128">Griffey's方法,但似乎无法得到它的工作。有一个关于如何使用类的信息非常少。我相信我导入位图不正确。如果有人知道如何得到这个工作,或者知道一个更好的办法,我会非常AP preciate你的帮助。
I am trying to create nine patch images programmatically. I have tried Griffey's approach but can't seem to get it to work. There is very little information about how to use the class. I believe I am importing the bitmap incorrectly. If anyone knows how to get this to work or knows a better way, I'd greatly appreciate your help.
我的code:
// Dynamically create the 9patch image and apply it to a LinearLayout
LinearLayout llcode = (LinearLayout) findViewById(R.id.llcode);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.white_button);
Drawable np = NinePatchBitmapFactory.createNinePathWithCapInsets(getResources(), bitmap, 15, 15, 16, 16, null);
llcode.setBackgroundDrawable(np);
// The following code using the actual 9patch image: white_button_np.9.png
LinearLayout llxml = (LinearLayout) findViewById(R.id.llxml);
llxml.setBackgroundDrawable(getResources().getDrawable(R.drawable.white_button_np));
该屏幕截图显示在顶部,并使用在底部的机器人工具的典型9patch方法的编程方法。
This screenshot shows the programmatic method on top and the typical 9patch method using the android tool on bottom.
在它原始图像会转换为一个9patch。
The original image before it's converted to a 9patch.
推荐答案
解决的办法是在这里: http://stackoverflow.com/ A /二十三万八千八百四十五分之一千六百六十七万六千四百一十九。我已经在API级别10模拟器成功地进行了测试。
The solution is here: http://stackoverflow.com/a/16676419/238845. I've tested it successfully on the API level 10 emulator.
这篇关于创建一个九补丁图像code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!