2

I am receiving nine-patch images from a server to display in the ui of my android app. This is a hard requirement so I am unable to compile the nine-patches into the APK. It appears that Android will recognize and display any PNG as a nine-patch, meaning the stretching of the image follows the outline indicated by the outer pixel. The problem is that Android still displays the 1px boundary of the file. So I have attempted to set the padding on the View to -1 so that line is not displayed. This does not appear to work since the black lines are still showing.

    <ImageView
        android:id="@+id/stub_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:padding="-1px"
        android:cropToPadding="true"
        android:src="@drawable/stub_image"
        android:contentDescription="@string/image_title" />

How can I set this layout to NOT show a 1px boundary around a PNG? Can I do this without overriding the View class and manually mucking around with pixels?

SOLUTION

Following the first option on the post @alex linked to, I am compiling the ninepatch files in an otherwise empty solution. Those are then provided to the app from my server. Since png's are possibly sent by the server, as well as ninepatches, the code I use to obtain the drawable is as follows:

InputStream stream = .. //whatever
Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] chunk = bitmap.getNinePatchChunk();
if (NinePatch.isNinePatchChunk(chunk)) {
    return new NinePatchDrawable(getResources(), bitmap, chunk, new Rect(), null);
} else {
    return new BitmapDrawable(getResources(), bitmap);
}
click_whir
  • 427
  • 5
  • 19
  • I don't think it loads the images as 9-patch drawables if the border is still shown. Your are using centerCrop as scaleType which scales the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). In other words it stretches the image to fill the drawing area completely without changing the aspect ratio, which might look as if it's drawn like a 9-patch drawable. What extension do the PNGs from your server have? – Emanuel Moecklin Apr 30 '13 at 00:09
  • 1
    If I change the layout to wrap_content and drop the centerCrop it will actually collapse the whole image down to the non-stretching region. – click_whir Apr 30 '13 at 01:16

1 Answers1

1

Use a NinePatchDrawable to parse the Bitmap so it's displayed properly.

alex
  • 6,359
  • 1
  • 23
  • 21
  • It is not very well documented, but my understanding was that the chunk provided to the NinePatchDrawable constructor is compiled nine-patch data, which I am unable to do in-app. – click_whir Apr 30 '13 at 01:32
  • Turns out this is more complex than I thought. See this answer: http://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime/5519768#5519768 – alex Apr 30 '13 at 01:56
  • I went with option 1) in the linked question. Thanks for the lead! – click_whir May 15 '13 at 06:23