Create nine patch images with code

I am trying to create nine patch images programmatically. I tried using the Griffey approach, but it seems like it can't get it to work. There is very little information on how to use the class. I am sure that I am importing the bitmap incorrectly. If anyone knows how to make this work or knows a better way, I really appreciate your help.

My code is:

// 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));

This screenshot shows a programming method on top and a typical 9patch method using the android tool at the bottom.

enter image description here

The original image before converting it to 9patch.

enter image description here

+5
source share
2 answers

Have you tried to make the decoder configuration make sure there is an alpha channel?

inPreferredConfig ARGB_8888.

0
0

All Articles