1

I was following the accepted answer of this question: Custom ImageView with drop shadow to add a shadow to my simple app's image view. I am reusing the exact the same drawable 9-patch image from the answer, and here is my code:

<ImageView
        android:id="@+id/photo"
        android:background="@drawable/imageview_shadow"
        android:paddingLeft="6px"
        android:paddingTop="4px"
        android:paddingRight="8px"
        android:paddingBottom="9px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:src="@drawable/test_img" />

It looks good on ADT preview 720P screens, but when I install it to my Nexus 5 which has 1080P resolution, it became like this:

enter image description here

Which looks pretty bad. I wasn't aware of 9-patch images could behave differently under different screen resolutions. So what could be the issue the shadow is not showing correctly?

Thank you

Community
  • 1
  • 1
Allan Jiang
  • 11,063
  • 27
  • 104
  • 165

1 Answers1

0

I believe the following line will take the whole .png file, including it's 1 pixel border and use it as a bitmap.

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.myNinePatchDrawable);

Try replacing this with the following to first extract the image from the NinePatch:

NinePatch myNinePatch = (NinePatch)getResources().getDrawable(R.drawable.myNinePatchDrawable);
Bitmap bmp = myNinePatch.getBitmap();
Sound Conception
  • 5,263
  • 5
  • 30
  • 47