編輯:關於 Android 程式設計
剛接觸 Android 時,我以為 XML 只能用來寫布局。後來才發現它的能力遠不止於此——可以存字串、可以配置列表,甚至能直接畫出一張圖。本文就來談談 XML 怎麼變成圖形。
XML 繪圖檔雖然寫的是布局語法,但必須放在 drawable 目錄下,放進 layout 目錄是不會生效的。這一步踩過坑的人應該不少,網路上卻很少有文章提到。
最簡單的一種,幾乎沒有技術含量:
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
android:src="@drawable/abc" />
在布局檔中像引用普通圖片一樣使用即可:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/circle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/draw"
android:text="Hello world"
android:textSize="50dp" />
</LinearLayout>
Shape 支持 rectangle、oval、line、ring 四種基本形狀,並可組合漸變、圓角、內距、描邊等屬性:
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" />
<gradient
android:angle="45"
android:startColor="#FF0000"
android:centerColor="#00FF00"
android:endColor="#0000FF"
android:type="linear" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
<size android:width="100dp" android:height="100dp" />
<stroke
android:width="2dp"
android:color="#333333"
android:dashWidth="4dp"
android:dashGap="2dp" />
</shape>
只對 rectangle 有效,可以分別指定四個角的半徑。當半徑設得夠大(例如 200dp),就會變成弧形邊。
color 指定顏色,width 指定寬度;dashWidth 與 dashGap 分別控制虛線的線段長度與間距。
Photoshop 的圖層概念在 Android 中同樣可以實現,多個 item 會依序往下疊:
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/abc" />
<item android:left="2dp" android:top="4dp">
<shape>
<solid android:color="@android:color/darker_gray" />
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
Selector 用來實作互動回饋——依據控制項當前狀態顯示不同圖形:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 沒有焦點時 -->
<item android:drawable="@drawable/pic1"
android:state_window_focused="false" />
<!-- 非觸摸模式下獲得焦點並按下 -->
<item android:drawable="@drawable/pic2"
android:state_focused="true" android:state_pressed="true" />
<!-- 觸摸模式下按下 -->
<item android:drawable="@drawable/pic3"
android:state_focused="false" android:state_pressed="true" />
<!-- 選中時 -->
<item android:drawable="@drawable/pic4"
android:state_selected="true" />
<!-- 獲得焦點時 -->
<item android:drawable="@drawable/pic5"
android:state_focused="true" />
<!-- 默認 -->
<item android:drawable="@drawable/pic1" />
</selector>
這幾種標籤可以自由嵌套組合,例如 Selector 中的每個 item 都可以是一段 Shape,从而实现「按下時變色、圓角保持不變」這類效果。熟練之後,大量原本需要切圖的介面元素都能用幾行 XML 搞定,/apk 體積與記憶體佔用都會更好看。
《Android 源碼設計模式解析與實戰》讀書筆記(二十六):MVP 應用架構模式
第二十六章、MVP 應用架構模式 1. MVP 介紹 MVP 模式是 MVC 模式的一個演化版本,全稱 Model-View-Presenter。目前 MVP 在 An
ScrollView 嵌套 ListView 只顯示一項?三種解法與官方建議
ListView 和 GridView 大家都用得很熟了,摯愛新技術的朋友大概已經全面轉向 RecyclerView。但只要還在用 ListView,你就一定遇過這樣的場
AndroidAnnotations 框架入門與 Android Studio 配置詳解
最近手邊的事情比較多,也有好一陣子沒更新部落格了。這兩天研究了名為 AndroidAnnotations 的開源框架,實際用起來相當順手,寫法讓人聯想到 Spring 的
Android 實現圖片倒影效果:Matrix 翻轉搭配漸層遮罩
要做出水面上那種倒影感,關鍵是兩件事:把原圖垂直翻轉貼在下半部,再從上到下疊一層由不透明漸/to 透明的漸層,讓倒影越往下越淡。 效果預覽 核心程式碼 全透明