创造&在 Android 中使用 9-patch 图像 [英] Creating & Using 9-patch images in Android

查看:48
本文介绍了创造&在 Android 中使用 9-patch 图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近听说了 9-patch 图像.我知道它的 9 块瓷砖是可拉伸的.我想了解更多.

  • 如何创建 9 块图像?

    有什么工具吗?我可以从 AndroidSDK 或代码创建它吗?

  • 9-patch 与常规 png 相比的主要优势?

    (是否可以根据屏幕动态/自动拉伸?)

解决方案

SDK 和 Android Studio 都附带了Draw 9-patch"工具(SDK 工具文件夹中的draw9patch"),它是一个简单的编辑器.

但是文档缺乏很好的例子.本教程最后有一些很好的例子回答问题的第二部分,解释缩放的工作原理 - 不仅适用于按钮 - 还适用于框架,并且它有一个完整的示例项目,您可以下载和使用.

I recently heard about 9-patch images. I know its 9 tiled and is stretchable. I'd like to know more about it.

  • How can I create a 9-patch image?

    Is there any tool? Can I create it from AndroidSDK or code?

  • Main Advantages of 9-patch over regular png?

    (is it stretchable dynamically/ automatically according to screen?)

解决方案

The SDK and Android Studio both ship with the "Draw 9-patch" tool ("draw9patch" in the SDK tools folder) which is a simple editor. Here is a nicer one which is also open source. It has a simple but clever default image.

The official documentation has improved over the years. In summary, nine patch images' most important advantage is that they can specify (non-contiguous) areas to scale:

A NinePatch graphic is a standard PNG image that includes an extra 1-pixel border. It must be saved with the 9.png extension in the res/drawable/ directory of your project.

Use the border to define the stretchable and static areas of the image. You indicate a stretchable section by drawing one (or more) 1-pixel wide black line(s) in the left and top part of the border (the other border pixels should be fully transparent or white). You can have as many stretchable sections as you want. The relative size of the stretchable sections stays the same, so the largest section always remains the largest.

You can also define an optional drawable section of the image (effectively, the padding lines) by drawing a line on the right and a line on the bottom. If a View object sets the NinePatch graphic as its background and then specifies the view's text, it stretches itself so that all the text occupies only the area designated by the right and bottom lines (if included). If the padding lines aren't included, Android uses the left and top lines to define this drawable area.

But the docs lack good examples. This tutorial has some great examples at the end that answer the second part of your question, explaining how the scaling works - not just for buttons - but also frames, and it has a complete example project that you can download and play with.

这篇关于创造&在 Android 中使用 9-patch 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆