AndyAndroid
AndyAndroid

Reputation: 4069

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

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.

Upvotes: 8

Views: 20690

Answers (4)

Shilpi
Shilpi

Reputation: 508

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

Upvotes: 0

Brian Rak
Brian Rak

Reputation: 5154

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.

Upvotes: 1

ashakirov
ashakirov

Reputation: 12360

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" />

Upvotes: 56

Anthony Graglia
Anthony Graglia

Reputation: 5435

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

Upvotes: 22

Related Questions