編輯:關於 Android 程式設計
所謂適配,就是讓同一個應用在不同的手機上呈現一致的視覺效果。動手之前,第一步永遠是先確認當前設備屬於哪一檔像素密度(xhdpi、hdpi、mdpi 等)。本文以華為 G700 與模擬器為例,示範密度計算與五種常見適配手段。
先求出屏幕對角線上的像素總數,再除以對角線英寸數:
案例一:G700 分辨率 1280*720,屏幕 5 英寸 sqrt(1280*1280 + 720*720) / 5 ≈ 293.72dpi 最接近 320dpi,歸入 xhdpi,1dp = 2px 案例二:模擬器 分辨率 800*480,屏幕 3.7 英寸 sqrt(800*800 + 480*480) / 3.7 ≈ 252.15dpi 最接近 240dpi,歸入 hdpi,1dp = 1.5px
對照 Google 官方文檔,市場上的設備大致可分為五檔:
ldpi :120dpi,1dp = 0.75px mdpi :160dpi,1dp = 1px hdpi :240dpi,1dp = 1.5px xhdpi :320dpi,1dp = 2px xxhdpi:480dpi,1dp = 3px

不同密度的設備加載 res 下不同文件夾的圖片資源。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a" />
</RelativeLayout>
G700(xhdpi)加載 res/drawable-xhdpi 下的 a.jpg:

模擬器(hdpi)加載 res/drawable-hdpi 下的 a.jpg:

在 res 下建立多套 values 目錄(如 values-1280x720、values-800x480),各自定義同名尺寸值。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="@dimen/width"
android:layout_height="wrap_content"
android:background="#987654"
android:text="@string/hello_world" />
</LinearLayout>
模擬器(hdpi,values-800x480):<dimen name="width">160dp</dimen>,換算後為 240px。
G700(xhdpi,values-1280x720):<dimen name="width">180dp</dimen>,換算後為 360px。兩者都能讓控件落在屏幕中間位置。
G700(xhdpi)效果:

模擬器(hdpi)效果:

為不同分辨率準備不同的 layout 目錄,例如 layout-1280x720 與 layout-800x480,各放一份 activity_main.xml。
模擬器(res/layout-800x480/activity_main.xml):
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="800*480 手機會加載的布局" />
G700(res/layout-1280x720/activity_main.xml):
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1280*720 手機會加載的布局" />
G700(xhdpi)效果:

模擬器(hdpi)效果:

透過 API 拿到屏幕的實際寬高,再按比例設定控件的 LayoutParams。
TextView tv = (TextView) findViewById(R.id.tv);
// 封裝屏幕信息的對象
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Constant.srceenHeight = metrics.heightPixels;
Constant.srceenWidth = metrics.widthPixels;
// 寬高各佔 50%
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(int) (Constant.srceenWidth * 0.5 + 0.5),
(int) (Constant.srceenHeight * 0.5 + 0.5));
tv.setLayoutParams(lp);
G700(xhdpi)效果:

模擬器(hdpi)效果:

借助 LinearLayout 的剩餘空間分配能力,讓子控件按比例瓜分屏幕,這是列表與分欄布局裡最省心的做法。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#123456" />
</LinearLayout>
G700(xhdpi)效果:

模擬器(hdpi)效果:

Android 進程間通信:用 AIDL 跨進程傳遞自訂物件
前言 AIDL 編程的基本步驟已在上一篇中介紹過(Android Studio 下的 AIDL 跨進程通信)。那篇只示範了如何傳遞 Java 基本型別與 Bitmap,但
Intent + Bundle 傳遞自訂物件:Serializable 與 Parcelable 全方位比較
Android 開發裡,Activity 之間傳遞資料幾乎是日常。基本型別用 putExtra 就夠,但如果要傳的是自訂物件,就必須先讓它「可序列化」。Android 支
Android 進程間通信入門:理解 Binder 與 AIDL 的關係
前言 在 Android 中,AIDL 常被用來實作進程間通信,但它其實只是工具,真正支撐通信機制的基礎是 Binder。本文從概念出發,說明 Client、Binder
深入解析 Android 5.0 波紋(Ripple)效果的實現與自定義
自從 Material Design 問世以來,波紋(Ripple)點擊反饋效果就成了大家耳熟能詳的互動體驗。用過 5.0 新控件的朋友對它應該不陌生,但如果換成 Tex