使用代码创建九个补丁图像

Bre*_*dan 5 android

我试图以编程方式创建九个补丁图像.我尝试过Griffey的方法,但似乎无法让它发挥作用.关于如何使用该课程的信息非常少.我相信我错误地导入了位图.如果有人知道如何让这个工作或知道更好的方式,我将非常感谢你的帮助.

我的代码:

// 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));
Run Code Online (Sandbox Code Playgroud)

此屏幕截图显示了顶部的编程方法和底部使用android工具的典型9patch方法.

在此输入图像描述

原始图像在转换为9patch之前.

在此输入图像描述

Chr*_*nes 0

您是否尝试过强制解码器配置以确保存在 Alpha 通道?

尝试将inPreferredConfig设置为ARGB_8888