編輯:Android 開發實例
在 drawable 目錄下建立如下檔案:
<bitmap xmlns:android="https://schemas.android.com/apk/res/android"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:mipMap="false"
android:src="@drawable/ic_launcher"
android:tileMode="disabled" />
各屬性說明(以下排版僅為方便閱讀,不可直接複製使用):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="1px"
android:bottomRightRadius="1px"
android:radius="2px"
android:topLeftRadius="1px"
android:topRightRadius="1px" />
<gradient
android:angle="5"
android:centerColor="#ff789434"
android:centerX="x"
android:centerY="y"
android:endColor="#34234324"
android:gradientRadius="55dp"
android:startColor="#ff783247"
android:type="linear"
android:useLevel="true" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<size android:width="70dp" android:height="70dp" />
<solid android:color="#fffff333" />
<stroke
android:width="30dp"
android:color="#33333333"
android:dashGap="4dp"
android:dashWidth="4dp" />
</shape>
<shape>:定義圖形形狀,有 rectangle(矩形)、oval(橢圓)、line(橫線)、ring(圓環)四種,預設矩形。使用 line 與 ring 時必須透過 stroke 指定線寬與顏色,否則顯示會異常。設為 ring 時另有五個專屬屬性:innerRadius(內半徑,與 innerRadiusRatio 同時存在時以它為準)、thickness(環的厚度,即外半徑減內半徑)、innerRadiusRatio(內半徑佔整體寬度比例,預設 9)、thicknessRatio(厚度佔整體比例,預設 3,算法為厚度 = 寬度 / n)、useLevel(一般為 false,除非當作 LevelListDrawable 使用)。
<corners>:四個角的角度,僅適用矩形,單位 px。radius 可統一設定四角但優先級較低;topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別對應左上、右上、左下、右下。
<gradient>:漸變填充,與 solid 互斥。angle 為漸變角度(必須是 45 的倍數,預設 0);centerX / centerY 為漸變中心座標;startColor / centerColor / endColor 為起始、中間與結束色;gradientRadius 為漸變半徑;type 有 linear(線性,預設)、radial(徑向)、sweep(掃描線)三種;useLevel 一般為 false,僅在 type 為 radial 時才需要。
<solid>:用 android:color 指定純色填充。
<stroke>:描邊。width 為線寬,color 為顏色,dashWidth 為虛線線段寬度,dashGap 為線段間距。
<padding>:內邊距,用法與 View 的 padding 相同,含 top / bottom / left / right 四個屬性。
<size>:width 與 height 定義尺寸,行為類似圖片本身的寬高,套用時會視情況縮放。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:bottom="1dp"
android:drawable="@drawable/ic_launcher"
android:left="1dp"
android:right="1dp"
android:top="1dp">
</item>
</layer-list>
一個 layer-list 可包含多個 item,每個 item 代表一個 drawable。上下左右四個屬性是相對於 view 的偏移量(像素),可用 drawable 直接引用資源,也可以在 item 內部自行定義。預設所有 drawable 都會被縮放到 view 的大小,且後面(下方)的圖層會覆蓋前面的。合理的分層能做出不少特殊疊加效果。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android"
android:constantSize="true"
android:dither="true"
android:variablePadding="true">
<item
android:drawable="@drawable/ic_launcher"
android:state_pressed="true"
android:state_focused="true"
android:state_hovered="true"
android:state_selected="true"
android:state_checkable="true"
android:state_checked="true"
android:state_enabled="true"
android:state_activated="true"
android:state_window_focused="true">
</item>
</selector>
constantSize 表示尺寸是否固定不隨狀態改變(預設 false);dither 建議開啟以改善低解析度螢幕的顯示;variablePadding 表示內邊距是否隨狀態改變。可用的狀態包含 pressed(按下未鬆開)、focused(已取得焦點)、hovered(游標懸停,4.0 之後)、selected、checkable、checked、enabled、activated 與 window_focused(App 是否在前台)。
關鍵規則:多個 item 時程式會由上到下逐一比對,先命中者生效(不是最佳匹配)。而沒有任何狀態說明的 item 可被任何狀態匹配,所以預設項必須放在最後一條,否則下面的選項永遠不會生效。
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="https://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/ic_lunch"
android:maxLevel="2"
android:minLevel="1">
</item>
</level-list>
集合中每個 drawable 都有一個等級區間。當它作為 view 背景時,可呼叫 Drawable 的 setLevel() 切換,ImageView 則可用 setImageLevel()。等級範圍是 0 到 10000,0 為預設值。
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/drawable1" />
<item android:drawable="@drawable/drawable2" />
</transition>
把它設為背景:
<TextView
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/transition_drawable" />
再在程式碼中觸發:
TextView textView = (TextView) findViewById(R.id.button); TransitionDrawable drawable = (TransitionDrawable) textView.getBackground(); drawable.startTransition(1000);
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_launcher"
android:insetBottom="2dp"
android:insetLeft="2dp"
android:insetRight="2dp"
android:insetTop="2dp" >
</inset>
把一個 Drawable 內嵌在自己內部並於四周留出間距,常用於「希望背景比實際區域小一點」的場合。
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_launcher"
android:scaleGravity="center"
android:scaleHeight="50%"
android:scaleWidth="50%" >
</scale>
使用時必須設定等級,否則不會生效:
View testScale = findViewById(R.id.test_scale); ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground(); testScaleDrawable.setLevel(1); // 範圍 1~10000,只要非 0 即可
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="https://schemas.android.com/apk/res/android"
android:clipOrientation="horizontal"
android:drawable="@+id/icon"
android:gravity="top" >
</clip>
gravity 與 clipOrientation 搭配決定從哪一側開始裁剪:
同樣需要在程式碼中設定等級:
ImageView testClip = (ImageView) findViewById(R.id.test_clip); ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable(); // 等級 0 表示完全裁剪,10000 表示完全不裁剪 testClipDrawable.setLevel(5000);
掌握這幾種標籤,多數常見的圓角按鈕、漸層背景、進度條與狀態切換效果,都能純靠 XML 完成,不必額外寫繪製程式碼。
Android 面試常考:gravity 與 layout_gravity 到底差在哪
這兩個屬性名字只差一個前綴,作用卻完全不同,是初學者最容易混淆的一組: android:gravity:決定控制項內部內容的對齊方式。例如按鈕裡那行文字要貼左、居中還是
Android AppWidgetProvider 的使用介紹
AppWidgetProvider 主要用於在桌面顯示插件。開發時通常需要準備一個 appwidget-provider 配置檔,用來宣告初始布局、最小寬高與更新週期,再
Android 音訊播放全攻略:MediaPlayer 方法與實作
要在 App 裡放音樂或播影片,MediaPlayer 是最經典的選擇。它位於 android.media 套件,能播放 SD 卡裡的檔案、res/raw 中的資源,也能
Android 圖像繪製:在畫布上縮放時的位置補償問題
在 Android 中對畫布上的圖像做縮放時,圖像本身的邊框尺寸並不會跟著改變。 原圖如下: 放大後:原本圖片的邊框沒有變,位置也還在原地。 這是矩陣變換的特性:Ma