第六節視頻的內容,真正做起來才發現自己一點都不了解這個點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>
記住那些要注意的地方,自己上機試一下,大概也就懂了。