Set 9patch dynamic background

Has anyone tried to set the dynamic background of the 9Patch button? If this is important, the width and height of the button are set by wrap_content

If so, how did you solve the black line problem?

thanks

+4
source share
1 answer

If you see black dots that you drew on the 9-patch image, this is because you did not precompile your images.

To set 9-patch images as background at runtime, they must be precompiled when the border is removed and encoded into a PNG block.

You can do this by pasting .9.png images into the res/drawable your application, compiling it and creating .apk (in Eclipse, right-click on project root > Android Tools > Export Unsigned Application Package ). Then unzip .apk and you will have .9.png images with 9 data patches collected in them.

With your precompiled images with 9 patches, do something similar to download the image:

 private Drawable loadNinePatch(String path, Context context) { Bitmap bitmap = BitmapFactory.decodeFile(path); byte[] chunk = bitmap.getNinePatchChunk(); if(NinePatch.isNinePatchChunk(chunk)) { return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null); } else return new BitmapDrawable(bitmap); } 

and then set it as the background of your button:

 button.setBackgroundDrawable(loadNinePatch("/data/data/your.app/files/button_background.9.png", context)); 
+5
source

Source: https://habr.com/ru/post/1432014/


All Articles