我正在尝试以编程方式创建9个补丁图像。我试过Griffey's的方法,但似乎无法使其工作。关于如何使用课程的信息很少。我相信我错误地导入了位图。如果有人知道如何让这个工作或知道更好的方法,我会非常感谢您的帮助。用代码创建9个补丁图像
我的代码:
// 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方法的编程方法。
原始图像,然后再将其转换为9patch。
经过一些更多的测试后,我发现上述错误只发生在旧手机和所有模拟器中。我在运行4.1.2的Galax Nexus上进行了测试,Griffey的课程运行良好。我在运行2.3.4的Droid X以及所有仿真器(包括Galaxy Nexus-4.1.2)上进行了测试,LinearLayout具有各种奇怪的颜色,如上面的屏幕截图。 – Brendan
我也被困在这一个,你找到一种方法来解决它? – Nativ
我已经用模拟器测试了这个按钮。在API级别10中,它似乎被破坏了,但在API级别16中,它对我来说确实很好。 – Scorchio