android 9-patch graphic doesn't scale in image view
Asked Answered
G

4

8

I have

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/ImageView01"
        android:src="@drawable/phone80"></ImageView>
    <ImageView android:id="@+id/ImageView02"
        android:layout_height="wrap_content" android:src="@drawable/underline"
        android:layout_width="fill_parent" android:layout_gravity="fill_vertical|fill_horizontal"></ImageView>
</LinearLayout>

The first image is a fixed size image which shouldn't scale at all. The image right of it should scale horizontally to the maximum space. The source points to a valid .9.png file. Unfortunatly it always only shows up in its original size. What property to I have to set? Is ImageView the right object at all?

Thanks, A.

Gca answered 9/3, 2011 at 8:0 Comment(0)
C
22

Use android:background instead. And if it is just an underline, you can use a View and not an ImageView. As for filling the remainder of the screen, you may want to use a TableLayout instead.

Also, make sure your 9-patch is correct. The 1px sides must be black. #000

Carbonyl answered 9/3, 2011 at 8:3 Comment(6)
Just got a downvote... Would love some feedback so others can benefit.Carbonyl
Adding an image to background instead of src means another layer of content which slows down performance. Wasn't my downvote, but I too want to be able to use a filler image and swap it with the final image when it loads; not have two images stacked.Mall
True and not true. You can add the background to the root linear layout and then you can have a padding or formatting on it as well meaning that the contents will be the second layer and easier to maintain... Both are great but it depends on use case. Thank you for the comment!Carbonyl
While this answer makes some good points, @almaz_from_kazan's answer is the better answer to this question.Kast
The answer below should be considered as the correct one, reason explained in the comment of the answer below.Honeywell
without scaleType="centerCrop" 9-patch image won't be shown as supposed. scaleType="centerCrop" works only for src="9patch path" (see #39131943), so I don't understand all this 9patch set to `background' if in this case all image stretches anyway (not only part with covered with black borders)Attenuate
W
56

Just add android:scaleType="fitXY" to your ImageView. For example for myimage.9.png:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/myimage"
    android:scaleType="fitXY" />
Whatnot answered 17/8, 2012 at 12:19 Comment(3)
this is correct. Without fitXY the 9 patch will not be honored and the image will just stretch as normal.Pentyl
Definitely the correct answer since android:background does not get considered when ImageView is inside RelativeLayout, and relative to other views.Honeywell
Just an addition to this answer. The scale was still wrong until I move the patch points from the left corner to the middleReardon
C
22

Use android:background instead. And if it is just an underline, you can use a View and not an ImageView. As for filling the remainder of the screen, you may want to use a TableLayout instead.

Also, make sure your 9-patch is correct. The 1px sides must be black. #000

Carbonyl answered 9/3, 2011 at 8:3 Comment(6)
Just got a downvote... Would love some feedback so others can benefit.Carbonyl
Adding an image to background instead of src means another layer of content which slows down performance. Wasn't my downvote, but I too want to be able to use a filler image and swap it with the final image when it loads; not have two images stacked.Mall
True and not true. You can add the background to the root linear layout and then you can have a padding or formatting on it as well meaning that the contents will be the second layer and easier to maintain... Both are great but it depends on use case. Thank you for the comment!Carbonyl
While this answer makes some good points, @almaz_from_kazan's answer is the better answer to this question.Kast
The answer below should be considered as the correct one, reason explained in the comment of the answer below.Honeywell
without scaleType="centerCrop" 9-patch image won't be shown as supposed. scaleType="centerCrop" works only for src="9patch path" (see #39131943), so I don't understand all this 9patch set to `background' if in this case all image stretches anyway (not only part with covered with black borders)Attenuate
T
1

Also check to make sure your scale lines on the sides don't extend all the way to the corners of the image. I knew it didn't really make sense for there to be black pixels there, but I figured Android would ignore them, but it actually prevents the scaling from working. It also gives you the "ERROR: 9-patch image xxx.9.png malformed" message in the output window, but you might not have noticed this. As soon as I took out the pixels in the corners, it started working for me.

Tabbi answered 30/10, 2013 at 18:8 Comment(0)
I
0

Try using setImageResource(9patchResourceID) instead of setImageBitmap or setImageBackground and it works :)

Impropriety answered 16/10, 2015 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.