【Android】点9图,点九图

2015-11-18  本文已影响4743人  Xxxxx面

第六节视频的内容,真正做起来才发现自己一点都不了解这个点9图。

概述

官网截的:

A NinePatchDrawable
graphic is a stretchable bitmap image, which Android will automatically resize to accommodate the contents of the View in which you have placed it as the background. An example use of a NinePatch is the backgrounds used by standard Android buttons — buttons must stretch to accommodate strings of various lengths. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension .9.png
, and saved into the res/drawable/
directory of your project.

点9图示一种可伸缩的位图,如果你某个View用了点9图做background,
Android会根据点9图的设置来自动为你调整、适应内容。
点9图是标准的PNG格式图像,被一圈1像素宽的边缘包围,
并且保存的时候扩展名一定要是“.9.png”,还要保存在“res/drawable/”目录。

其它要注意的地方

1.四边那几条黑线代表什么

2.最外围的一圈像素必须要么是纯黑色,要么是透明,一点点的半透明的像素都不可以有,比如说99%的黑色或者是1%的投影都不可以有

3.文件的后缀名必须是.9.png,不能是.png或者是.9.png.png,这样的命名都会导致编译失败。

关于操作

真正操作起来,才发现自己真的很不熟练。
看视频的时候,说“Create 9-Patch file”,
然后我连Create 9-Patch file在哪里点击都没找到……

之前是用那种画点9图的工具,现在AndroidStudio直接就有提供了。
把png格式(一定要是png,jpg之类的都不行),复制到“res/drawable/”目录,
点击右键,就会在菜单看到Create 9-Patch file了。

留意下面还有 □Show content 和 □Show patches 两个选项。


画的时候,就是在那1像素的包围圈“拖动”划线和“按住Shift键”删除。


我的上机试验

我做了一个EditText和Button,
那个Button的background就是下面这幅图,
把它像上面那样做成点9图。



当在EditText输入字符串,然后点击Button,
Button就会显示输入的内容,然后我就输入很多字符。
分别设置点9图上下左右的黑线,看Button会被撑开成什么样子。

代码如下:

public class AnotherActivity extends ActionBarActivity {

    private Button btn_testNinePatch;
    private EditText et_testNinePatch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
        btn_testNinePatch = (Button) findViewById(R.id.btn_testNinePatch);
        et_testNinePatch = (EditText) findViewById(R.id.et_testNinePatch);
        btn_testNinePatch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_testNinePatch.setText(et_testNinePatch.getText().toString());
            }
        });
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_testNinePatch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_testNinePatch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:text="sdfdsfdsfdsfdsfdsfdsfdsfsdf" />

</LinearLayout>

记住那些要注意的地方,自己上机试一下,大概也就懂了。

参考资料

  1. Canvas and Drawables
  2. Draw 9-patch
  3. 【推荐】A simple guide to 9-patch for Android UI
  4. Android设计中的.9.png
  5. Android 点9图片制作及讲解
  6. 点九图简单介绍及制作教程
  7. 关于”点九”
上一篇 下一篇

猜你喜欢

热点阅读