Nine patch image not working on my device?

China☆狼群 提交于 2019-11-26 21:58:03

问题


This is the first time i use nine-patch image on my android app. It so stupit. When I preview on eclipse layout editor, it work. But when I build the application on real device (HTC Inspire 4g, HTC Explorer,...) it's not.

<LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_margin="5dp"
                        android:layout_weight="1"
                        android:background="@drawable/shadow_border"
                        android:gravity="center"
                        android:padding="0dip" >

                        <TextView
                            android:id="@+id/itemdetail_textview_price"
                            style="@style/textview_nomalBold"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="$10"
                            android:textColor="#F00"
                            android:textSize="32dp" />

                        <TextView
                            android:id="@+id/itemdetail_textview_originalPrice"
                            style="@style/textview_nomalBold"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="15dp"
                            android:text="$20"
                            android:textSize="32dp" />
                    </LinearLayout>

Is there any problem here?

UPDATED:

  • I am writing the app for Android 2.2 and above.

  • My Phones use Android 2.3


回答1:


Remove two dots from bottom and right side because these side decides in how much space your content should be displayed.

and here is how the nine patches work in android

And Follow this tutorial when you create a nine patch image.




回答2:


I solved it by my self but I don't understand why it works.

In nine-patch, I draw 2 line at the top and the left of image (see 2 black lines at the picture)




回答3:


I tested it more deeply, and it seems that there are inconsistencies between the draw9patch tool, Graphical editor in Eclipse and the device. Nine-patches, that seem to work in the first two, do not work on the device - they silently fall back to BitmapDrawable, without reporting an error. Reproduces on my 2.3.5 device, as well as in 4.1 emulator. See this android bug: http://code.google.com/p/android/issues/detail?id=38941

Workaround is to draw single row of pixels, not just dots.

TIP: your image should be as small as possible. If you put two dots to the left or top region, it means, that you have two stretched areas and the area between them is kept unstretched. You can cut out the area from the first dot (excluded) to the second dot (included) and have only one dot, with a smaller image and the same result.




回答4:


The image I'm using has a shadow, and brilliantly I assumed that it ends before it reaches the last pixel of the image... and so didn't bother to enlarge the image by a pixel... (I know the 9 patch tool is doing this for me but I was in the middle of editing the image so I just added it there)

So the problem is The preview can handle pixels with alpha, the 9-patch tool can handle it as well, but the device cannot!



来源:https://stackoverflow.com/questions/10479001/nine-patch-image-not-working-on-my-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!