Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發 Tips(3):10 個提升效率的工具與寫法

編輯:關於 Android 程式設計

這一則是零碎但實用的小技巧合集,涵蓋測試工具、Android Studio 插件、網路層寫法與效能優化,每一項都拿去就能用。

1. UIAutomatorViewer:快速拿到控件的 resource-id

自動化測試已是 Android 測試的主流方向,穩定、可複用,最常用的框架是 Espresso。而寫測試的第一步,永遠是「這個控件的 id 是什麼」。UIAutomatorViewer 就是為此而生,位置在 android-sdk/tools/uiautomatorviewer,直接執行即可開啟圖形化檢視。

2. 善用 GitHub 的 Topics 標籤

搜尋開源庫時,除了關鍵字,也可以直接從仓库頁面的 Topics 標籤切入,往往能一次找到一整批同類型的優秀專案。

3. 動態修改 SVG 顏色的庫

有些庫可以載入 SVG 圖片,並在執行期改寫其填色屬性。對於「同一個 icon 要在不同狀態換色」的需求,比準備好幾套 PNG 資源要優雅得多。

4. 兩個處理 JSON 的必備插件

  • JSONOnlineViewer:網路請求插件,可直接發請求並取得 JSON。位置在 View → JSONViewer。
  • GsonFormat:依 JSON 結構自動產生對應的 Java 類別,快捷鍵 Command + N(Windows 為 Alt + Insert)。

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

5. Retrofit2 + OkHttp3 的 Interceptor 寫法變了

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 根本沒被註冊進去,等於沒帶上驗證資訊,私鑰再正確也沒用。

6. 讓 okhttp-logging-interceptor 真的輸出 log

依賴宣告(當前版本 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)

7. 透明 statusbar + 全屏 ImageView

先在主題中讓狀態列透明:

<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>

8. Android Studio 的 Live Templates

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

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

9. 找動畫效果靈感的網站

網路上有不少專門蒐集動畫效果的站點,裡面的效果幾乎都是以程式碼實作而非影片,因此移植到自己的專案相對容易,雪花飄落這類粒子效果尤其常見。

10. ListView 的 ViewHolder 別偷懶

官方雖然推薦以 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.

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved