2

I have a business need to create the NinePatchDrawable objects at runtime, this is, an exterior .png image is received from a server and it has to be applied in a button's background (for example) as a nine patch.

I have tried to create the NinePatchDrawable object, but the constructor asks me for a "byte[] chunck" that describes the patch. The thing is, I have no idea on how to build this chunk from a bitmap that does not have the 9patch information in it.

Any ideas on this topic? Am I seeing the problem from a wrong perspective?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Pedro Lopes
  • 21
  • 1
  • 2
  • Didn't you say that your server is generating a complete 9patch graphic? Including the boundaries (black borders etc). Have you tried calling it exactly like that? If the bitmap doesn't have the black borders, you need to improve the server software (in your case) – Sebastian Roth Dec 28 '10 at 16:01
  • I am generating the bitmap with the black borders in the android itself. The image that comes from the server is borderless. The problem is that I cannot extrapolate the 9patch chunk information from the Bitmap object – Pedro Lopes Dec 28 '10 at 16:05
  • @Pedro Lopes: Then how are you going to determine what pixels in the control frame should be black vs. white? – CommonsWare Dec 28 '10 at 16:13
  • Through a Style XML file that is given by the server. It works much like CSS – Pedro Lopes Dec 28 '10 at 16:19
  • @Pedro Lopes: Then have the server create the nine-patch PNG file. I still do not not understand how a "Style XML" file will determine the stretchable ranges in a nine-patch, but if the server has the image, and it has the "Style XML", then the server is a far better place to combine the two than would be your Android app. – CommonsWare Dec 28 '10 at 16:48
  • Ok, I tried the server alternative, this is, I tried loading a 9.png file from the outside, but still dont have any 9patch chunck information. The code that I tried: Bitmap bMap = BitmapFactory.decodeFile("/sdcard/DCIM/Camera/test.9.png"); bMapDrawable = new BitmapDrawable(bMap); if(NinePatch.isNinePatchChunk(bMap.getNinePatchChunk())) { // NINE PATCH INFORMATION IS PRESENT } else { // NO NINE PATCH INFORMATION :\ // This is where the code is going to } – Pedro Lopes Dec 28 '10 at 17:38

2 Answers2

2

See my answer for Create a NinePatch/NinePatchDrawable in runtime

I develop a tool to create NinePatchDrawable from (uncompiled) NinePatch bitmap. See https://gist.github.com/knight9999/86bec38071a9e0a781ee .

The method NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap) helps you.

For example,

    ImageView imageView = (ImageView) findViewById(R.id.imageview);
    Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
    NinePatchDrawable drawable = NinePatchBitmapFactory.createNinePatchDrawable(getResources(), bitmap);
    imageView.setBackground( drawable );

where

public static final Bitmap loadBitmapAsset(String fileName,Context context) {
    final AssetManager assetManager = context.getAssets();
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(assetManager.open(fileName));
        return BitmapFactory.decodeStream(bis);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (Exception e) {

        }
    }
    return null;
}

In this case, the my_nine_patch_image.9.png is under the assets directory.

Community
  • 1
  • 1
KNaito
  • 3,930
  • 2
  • 19
  • 21
1

I've answered this over at question 5519768. Keep in mind the differences between "source" and "compiled" ninepatch images.

Community
  • 1
  • 1
kanzure
  • 1,331
  • 11
  • 15