編輯:關於 Android 程式設計
這一則是零碎但實用的小技巧合集,涵蓋測試工具、Android Studio 插件、網路層寫法與效能優化,每一項都拿去就能用。
自動化測試已是 Android 測試的主流方向,穩定、可複用,最常用的框架是 Espresso。而寫測試的第一步,永遠是「這個控件的 id 是什麼」。UIAutomatorViewer 就是為此而生,位置在 android-sdk/tools/uiautomatorviewer,直接執行即可開啟圖形化檢視。

搜尋開源庫時,除了關鍵字,也可以直接從仓库頁面的 Topics 標籤切入,往往能一次找到一整批同類型的優秀專案。
有些庫可以載入 SVG 圖片,並在執行期改寫其填色屬性。對於「同一個 icon 要在不同狀態換色」的需求,比準備好幾套 PNG 資源要優雅得多。

這兩個搭配使用,寫資料層的速度會快一倍以上。

Retrofit 升上 beta3 之後改用最新的 OkHttp3,Interceptor 的註冊方式跟舊版完全不同。
舊版寫法:
OkHttpClient client = new OkHttpClient().Builder();
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
MarvelSigningInterceptor signingInterceptor = new MarvelSigningInterceptor(
BuildConfig.MARVEL_PUBLIC_KEY, BuildConfig.MARVEL_PRIVATE_KEY);
client.interceptors().add(signingInterceptor);
client.interceptors().add(loggingInterceptor);
新版寫法:
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
MarvelSigningInterceptor signingInterceptor = new MarvelSigningInterceptor(
BuildConfig.MARVEL_PUBLIC_KEY, BuildConfig.MARVEL_PRIVATE_KEY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(signingInterceptor)
.addInterceptor(loggingInterceptor)
.build();
照舊版寫而不改,很可能收到 HTTP 409 Conflict——因為 Interceptor 根本沒被註冊進去,等於沒帶上驗證資訊,私鑰再正確也沒用。
依賴宣告(當前版本 3.0.1):
compile "com.squareup.okhttp3:logging-interceptor:${libs.okhttp}"
輸出大致長這樣:
D/OkHttp: <-- 200 OK https://gateway.example.com/v1/public/characters?offset=0&... (1552ms, unknown-length body)
先在主題中讓狀態列透明:
<style name="AppTheme.NoStatusBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
頁面的根布局使用 CollapsingToolbarLayout,並讓圖片填滿整個視窗:
<android.support.design.widget.CollapsingToolbarLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@null"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/christmas" />
</android.support.design.widget.CollapsingToolbarLayout>

位置:File → Other Settings → Default Settings → Editor → Live Templates。熟練之後,只要簡寫 + Tab 就能展開整段程式碼,當然也可以自己新增。

自訂模板需要填的四個欄位:縮寫(Abbreviation)、描述(Description)、內容(Template text)、應用場景,最後記得指定格式化方式。

網路上有不少專門蒐集動畫效果的站點,裡面的效果幾乎都是以程式碼實作而非影片,因此移植到自己的專案相對容易,雪花飄落這類粒子效果尤其常見。
官方雖然推薦以 RecyclerView 取代 ListView,但維護老專案時你未必有得選。既然還要用 ListView,ViewHolder 就該寫上——它省下的 findViewById() 次數,在長列表滑動時是能被明顯感知的。
static class ViewHolder {
TextView testName;
TextView testDesc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.view_test_row, parent, false);
ViewHolder holder = new ViewHolder();
holder.testName = (TextView) rowView.findViewById(R.id.test_tv_name);
holder.testDesc = (TextView) rowView.findViewById(R.id.test_tv_desc);
rowView.setTag(holder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.testName.setText("Test: " + position);
holder.testDesc.setText("This is number " + position + ". ");
return rowView;
}
以上,Enjoy it.
深入分析 PMS 服務(一):從 PackageParser 看 APK 如何變成 Package
要理解 PackageManagerService(PMS),不妨先把它周邊的工具類看透。本文不直接切入 PMS 源碼,而是從 PackageParser 說起——它的職
Swift 錯誤處理魔法課:可空、guard、自定義錯誤與 defer
Swift 錯誤處理的起點 Swift 的錯誤處理從早期版本一路演進,到 Swift 2 之後,逐漸強調可恢復錯誤、可空型別與 guard 語句的組合使用。良好的錯誤處理
深入解析 Android 四大組件之 Activity
一、Activity 是什麼 Activity 是 Android 應用中負責用戶交互的核心組件,可以把它理解為應用程序的「界面單元」。每一個 Activity 都會創建
Android 多線程下載原理與實作:分段 + RandomAccessFile
單線程下載有個明顯的缺點:一條連接的速度往往吃不满帶寬。多線程下載的核心思路很簡單——把一個大文件切成若干段,每段用一條獨立連接去取,最後寫入同一個文件的對應位置。 一