Android開發Tips(3)

來源:互聯網
上載者:User

標籤:service   add   okhttp3   效果   tips   via   find   tomat   inflate   

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

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

imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Android" title="">

1. UIAutomatorViewer

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

2. GitHub標籤

網址, 比方:

3. 有趣的改動SVG庫

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

4. 請求和產生的Json外掛程式

JSONOnlineViewer, 網路請求外掛程式, 擷取Json資料, 位置View->JSONViewer.
GsonFormat, 依據Json自己主動產生類的外掛程式, 在Command+N裡面.

附一張外掛程式的, 其它任意.

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, 未輸入正確的驗證方式, 私密金鑰錯誤.

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="http://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模板

位置: 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;}...@Overridepublic 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;}

OK, that’s all! Enjoy it.

Android開發Tips(3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.