編輯:關於 Android 程式設計
厭倦了單調的純色背景嗎?Android 提供了兩條路徑來做漸層:一條是在 drawable 目錄寫個 XML,幾行搞定;另一條是走 android.graphics 底下那組 Shader 類,用程式碼精細控制。兩種都整理在這裡。
在 res/drawable 底下新增一個 XML 檔:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:startColor="#ffffff"
android:endColor="#000000" />
<corners
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip"
android:topLeftRadius="5dip"
android:topRightRadius="5dip" />
</shape>
幾個節點的職責:
寫好之後,無論在 XML 佈局的 background 屬性還是程式碼的 setBackgroundResource() 中都能直接引用。只要你知道 RGB 值,配色就能玩出花樣。
在 android.graphics 中,與漸層相關的類都繼承自 Shader,共三兄弟:LinearGradient(線性)、RadialGradient(徑向)、SweepGradient(角度/扇形)。使用時透過 Paint 的 setShader() 掛上去即可。
提供兩種建構方式,差別在於精度:
LinearGradient(float x0, float y0, float x1, float y1,
int[] colors, float[] positions, Shader.TileMode tile)
LinearGradient(float x0, float y0, float x1, float y1,
int color0, int color1, Shader.TileMode tile)
第一種可傳入顏色陣列與對應位置,做出多段細膩過渡——例如塞 20 種顏色進 colors,系統會逐段插值。第二種只吃起始色與結束色,夠用但比較樸素。
Paint p = new Paint();
LinearGradient lg = new LinearGradient(0, 0, 100, 100,
Color.RED, Color.BLUE, Shader.TileMode.MIRROR);
p.setShader(lg);
canvas.drawCircle(0, 0, 200, p);
前四個參數是漸層的起點與終點座標,最後的 TileMode 決定超出漸層範圍後如何鋪排,MIRROR 為鏡像重複,另有 CLAMP(邊緣色延伸)與 REPEAT(循環重複)。
與線性版唯一的差別,是第三個參數換成了半徑:
RadialGradient(float x, float y, float radius,
int[] colors, float[] positions, Shader.TileMode tile)
RadialGradient(float x, float y, float radius,
int color0, int color1, Shader.TileMode tile)
前兩個參數是圓心,radius 決定漸層向外擴散多遠,超過半徑的區域就交給 TileMode 處理。
SweepGradient(float cx, float cy, int[] colors, float[] positions)
以圓心為中心,顏色沿圓周掃過一圈。想做圓錐形或 3D 光暈效果時特別好用。官方對 positions 參數的說明是:
May be NULL. The relative position of each corresponding color in the colors array, beginning with 0 and ending with 1.0. If the values are not monotonic, the drawing may produce unexpected results. If positions is NULL, then the colors are automatically spaced evenly.
也就是說位置陣列必須單調遞增,否則結果會很怪。多數情況下傳 NULL 讓系統均勻分佈就好,或者直接用雙色版本:
SweepGradient(float cx, float cy, int color0, int color1)
如果不想自己 draw,也可以直接造一個 GradientDrawable 塞給視窗:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.login);
GradientDrawable grad = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.BLACK, Color.WHITE});
getWindow().setBackgroundDrawable(grad);
}
小結一下選型:只需要一個靜態漸層背景,寫 XML 最省事;需要跟著動畫變色、或者要畫在非矩形區域上,就走 Shader + Paint 這條路。
Android 5.0 期待功能:八個值得實現的改進
Android 5.0 在當時被視為重要更新。外界除了關注介面改版,也期待系統能在功能、生態與使用細節上更成熟。介面與功能傳聞新版介面被期待更扁平,圖標設計向 Chrom
Android 6.0 以上藍牙通訊與權限適配實作
Android 6.0 之後,藍牙相關功能除了硬體與權限外,還需要更完整的執行期授權流程。尤其搜尋附近裝置時,定位權限往往成為關鍵。本文整理藍牙通訊的基本流程與實作重點。
Android 動畫機制與技巧(五)——Android 5.X 的 SVG 向量動畫
Google 在 Android 5.X 中加入了對 SVG 向量圖形的支援,這對打造高效能的動畫意義重大。先來釐清 SVG 的概念。 可縮放向量圖形(Scalable
Android 關於頁面怎麼做:用 AlertDialog 擴充一個 AboutDialog
幾乎每個 App 都會有這麼一頁:版本號、作者、聯絡方式、簡介。它不需要獨立 Activity,用一個 AlertDialog 承載自訂版面就足夠,而且實作乾淨。 一