9patch 图片

经常用,在此做一记录:

简言之,做9patch图片的工具在tools下的draw9patch工具,然后左上画黑线的地方分别表示扩展的区域,拉伸的话会拉伸黑线部分,非黑线部分不做扩展

右下表示图片的显示区域,只显示黑线区域的内容,非黑线区域不显示,具体描述和图例如下:

Supporting different screen sizes usually means that your image resourcesmust also be capable of adapting to different sizes. For example, a buttonbackground must fit whichever button shape it is applied to.

If you use simple images on components that can change size, you willquickly notice that the results are somewhat less than impressive, since theruntime will stretch or shrink your images uniformly. The solution is using nine-patch bitmaps,which are speciallyformatted PNG files that indicate which areas can and cannot be stretched.

Therefore, when designing bitmaps that will be used on components withvariable size, always use nine-patches. To convert a bitmap into a nine-patch,you can start with a regular image (figure 4, shown with in 4x zoom for clarity).

Figure 4. button.png

And then run it through the draw9patch utility of theSDK (which is located in the tools/ directory), in which you can mark the areas thatshould be stretched by drawing pixels along the left and top borders. You can also mark the areathat should hold the content by drawing pixels along the right and bottom borders, resulting infigure 5.

Figure 5. button.9.png

Notice the black pixels along the borders. The ones on the top and leftborders indicate the places where the image can be stretched, and the ones onthe right and bottom borders indicate where the content should beplaced.

Also, notice the .9.png extension. You must use this extension, since this is how the framework detects that this is a nine-patchimage, as opposed to a regular PNG image.

When you apply this background to a component (by settingandroid:background="@drawable/button"), the framework stretchesthe image correctly to accommodate the size of the button, as shown in various sizes in figure6.

Figure 6. A button using the button.9.pngnine-patch in various sizes.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值