Android 9-patch

1.   9-patch介绍    

9-patch绘画的是一个可以伸缩的位图图像,Android会自动调整大小来容纳显示的内容。一个例子就是NinePatch为背景,使用标准的Android按钮,按钮必须伸缩来容纳长度变化的字符。

       9-patch是一个标准的PNG图像,它包括额外的1个像素的边界,你必须保存它后缀为.9.png,并且保存到工程的res/drawable目录中。如果你是从APK解压后得到的*.9.png文件,注意它是已将周围的空白像素去掉的,在使用时必须再加上,不然eclipse会报错。

2.   使用9-patch流程说明

 

1.      (1)From a terminal, launch the draw9patch application from your SDK /tools directory.

2.      (2)Drag your PNG image into the Draw 9-patch window (or File > Open 9-patch... to locate the file). Your workspace will now open.The left pane is your drawing area, in which you can edit the lines for the stretchable patches and content area. The right pane is the preview area, where you can preview your graphic when stretched.

3.      (3)Click within the 1-pixel perimeter to draw the lines that define the stretchable patches and (optional) content area. Right-click (or hold Shift and click, on Mac) to erase previously drawn lines.

4.      (4)When done, select File > Save 9-patch...

Your image will be saved with the .9.png file name.

 

Note: A normal PNG file (*.png) will be loaded with an empty one-pixel border added around the image, in which you can draw the stretchable patches and content area. A previously saved 9-patch file (*.9.png) will be loaded as-is, with no drawing area added, because it already exists.

Android  9-patch - 猫君 - 猫群博客
 

Optional controls include:

·         (1)Zoom: Adjust the zoom level of the graphic in the drawing area.

·         (2)Patch scale: Adjust the scale of the images in the preview area.

·         (3)Show lock: Visualize the non-drawable area of the graphic on mouse-over.

·         (4)Show patches: Preview the stretchable patches in the drawing area (pink is a stretchable patch).

·         (5)Show content: Highlight the content area in the preview images (purple is the area in which content is allowed).

·         (6)Show bad patches: Adds a red border around patch areas that may produce artifacts in the graphic when stretched. Visual coherence of your stretched image will be maintained if you eliminate all bad patches.

 

3.   使用9-patch问题及解决方法

1. draw9patch.bat不能运行的一个解决方案,下载jar包,地址是:
http://netmite.com/android/mydroid/1.6/prebuilt/common/swing-worker/swing-worker-1.1.jar
下好的jar包放到:$ANDROID_SDK/tools/lib 文件里,再运行就可以。

 

2.Android项目中使用Nine Patch资源,时候可能出现比如ERROR: 9-patch image android123.9.png malformed. No marked region found along edge.以及 Found along top edge.的错误主要是因为该图片没有四周的定位像素


参考:http://developer.android.com/guide/topics/graphics/2d-graphics.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些基本的步骤来自己设计一张9-Patch图片,并应用图片为窗体添加一个不失真背景。 1. 首先,您需要打开 Android Studio,并创建一个新的项目。 2. 接下来,您需要创建一个新的 drawable 资源文件夹,用于存放您的 9-Patch 图片。您可以右键点击 res 文件夹,选择 New -> Android Resource Directory。 3. 在弹出的对话框中,选择 drawable 作为资源类型,并为资源文件夹命名(例如:drawable-np)。 4. 选中新创建的 drawable-np 文件夹,右键点击选择 New -> Drawable Resource。 5. 在弹出的对话框中,为您的 9-Patch 图片命名,并选择文件类型为 9-Patch。 6. 在接下来的编辑器中,您可以为您的 9-Patch 图片添加拉伸和内容区域。拉伸区域用于指定背景图片的拉伸部分,而内容区域用于指定背景图片的内容部分。 7. 完成后,保存您的 9-Patch 图片。 8. 接下来,您需要在布局文件中将这个 9-Patch 图片作为窗体背景进行应用。例如,在您的 activity_main.xml 布局文件中,您可以添加以下代码: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/your_9_patch_image_name" > <!-- Your views here --> </RelativeLayout> ``` 9. 最后,重新运行您的应用程序,您应该可以看到您的窗体现在具有一个不失真的背景。 希望这些步骤可以帮助您自己设计一张9-Patch图片,并应用图片为窗体添加一个不失真背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值