Android開發Tips(3)

小小新 8年前發布 | 24K 次閱讀 Android開發 移動開發

來自: http://blog.csdn.net/caroline_wendy/article/details/50572915


歡迎Follow我的GitHub, 關注我的CSDN.

</blockquote>

我會介紹關于Android的一些有趣的小知識點. 本文是第三篇, 其余第一篇, 第二篇.

Android


1. UIAutomatorViewer

自動化測試是Android測試的趨勢, 穩定\復用, 最常用的工具就是Espresso.
使用UIAutomatorViewer獲取資源的Id,
位置/android-sdk/tools/uiautomatorviewer, 點擊即可使用.

視圖


2. GitHub標簽

網址, 比如:


3. 有趣的修改SVG庫

地址, 加載SVG格式的圖片, 修改顏色屬性.

Demo


4. 請求和生成的Json插件

JSONOnlineViewer, 網絡請求插件, 獲取Json數據, 位置View->JSONViewer.
GsonFormat, 根據Json自動生成類的插件, 在Command+N里面.

附一張插件的截圖, 其他隨意.

Plugins


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);</pre>

替換, 新版

        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();</pre> 

否則可能會發生: HTTP 409 Conflict, 未輸入正確的驗證方式, 私鑰錯誤.


6. okhttp-logging-interceptor輸出log信息

參考, 可以輸出log信息, 使用, 當前版本是3.0.1.

compile "com.squareup.okhttp3:logging-interceptor:${libs.okhttp}"

輸出參考:

D/OkHttp: <-- 200 OK http://gateway.marvel.com/v1/public/characters?offset=0&... (1552ms, unknown-length body)

7. 透明statusbar和全屏ImageView

status bar設置成為透明顏色.

    <style name="AppTheme.NoStatusBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

頁面的根布局是CollapsingToolbarLayout.

<android.support.design.widget.CollapsingToolbarLayout
    xmlns:android="

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

效果
全屏


8. Android Studio模板

位置: File->Other Settings->Default Settings->Editor->Live Templates
熟練之后, 根據簡寫+Tab就可以使用了, 當然也可以自己添加.

模板

自定義模板:
縮寫(Abbreviation), 描述(Description), 內容(Template text), 應用場景, 格式化.
自定義


9. 推薦動畫效果的網站

網址, 網站里面有很多好玩的動畫效果, 而且都是編程實現, 方便移植, 如雪花效果.


10. ListView的ViewHolder

Android官方推薦使用RecyclerView代替ListView, 但是很多守舊的人不想這么做, 那么, 也需要使用ViewHolder提升加載速度. 參考.

基本用法.

static class ViewHolder() {
    TextView testName;
    TextView testDesc;
}

...

@Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView;

// 初始化ViewHolder
if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(R.layout.view_test_row, parent, false);
    ViewHolder viewHolder = new ViewHolder();
    viewHolder.testName = (TextView) rowView.findViewById(R.id.test_tv_name);
    viewHolder.testDesc = (TextView) rowView.findViewById(R.id.test_tv_desc);
    rowView.setTag(viewHolder);
}

// 使用ViewHolder
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.testName.setText("Test: " + position);
holder.testDesc.setText("This is number " + position + ". ");

return rowView;

}</pre>


OK, that’s all! Enjoy it.

</div>

 本文由用戶 小小新 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!