Android開(kāi)發(fā)Tips(3)

歡迎Follow我的GitHub, 關(guān)注我的簡(jiǎn)書(shū). 其余參考Android目錄.

Android

本文的合集已經(jīng)編著成書(shū),高級(jí)Android開(kāi)發(fā)強(qiáng)化實(shí)戰(zhàn),歡迎各位讀友的建議和指導(dǎo)。在京東即可購(gòu)買(mǎi):https://item.jd.com/12385680.html

Android

我會(huì)介紹關(guān)于Android的一些有趣的小知識(shí)點(diǎn). 本文是第三篇, 其余第一篇, 第二篇.


1. UIAutomatorViewer

自動(dòng)化測(cè)試是Android測(cè)試的趨勢(shì), 穩(wěn)定\復(fù)用, 最常用的工具就是Espresso.
使用UIAutomatorViewer獲取資源的Id,
位置/android-sdk/tools/uiautomatorviewer, 點(diǎn)擊即可使用.

視圖

2. GitHub標(biāo)簽

網(wǎng)址, 比如:
[圖片上傳失敗...(image-ca4295-1530169346314)]
[圖片上傳失敗...(image-874431-1530169346315)]


3. 有趣的修改SVG庫(kù)

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

Demo

4. 請(qǐng)求和生成的Json插件

JSONOnlineViewer, 網(wǎng)絡(luò)請(qǐng)求插件, 獲取Json數(shù)據(jù), 位置View->JSONViewer.
GsonFormat, 根據(jù)Json自動(dòng)生成類(lèi)的插件, 在Command+N里面.

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

Plugins

5. Retrofit2+Okhttp3的Interceptor設(shè)置方式

Retrofit升級(jí)到beta3版本, 使用了最新Okhttp3, Interceptor的設(shè)置方式發(fā)生改變.

舊版

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

否則可能會(huì)發(fā)生: HTTP 409 Conflict, 未輸入正確的驗(yàn)證方式, 私鑰錯(cuò)誤.


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

參考, 可以輸出log信息, 使用, 當(dāng)前版本是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設(shè)置成為透明顏色.

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

頁(yè)面的根布局是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>

效果


效果

其他布局無(wú)法覆蓋效果, 參考.


8. Android Studio模板

位置: File->Other Settings->Default Settings->Editor->Live Templates
熟練之后, 根據(jù)簡(jiǎn)寫(xiě)+Tab就可以使用了, 當(dāng)然也可以自己添加.

位置

自定義模板:
縮寫(xiě)(Abbreviation), 描述(Description), 內(nèi)容(Template text), 應(yīng)用場(chǎng)景, 格式化.


自定義

9. 推薦動(dòng)畫(huà)效果的網(wǎng)站

網(wǎng)址, 網(wǎng)站里面有很多好玩的動(dòng)畫(huà)效果, 而且都是編程實(shí)現(xiàn), 方便移植, 如雪花效果.


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

OK, that's all! Enjoy it.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,191評(píng)論 25 708
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,892評(píng)論 2 45
  • 二十二歲了,人生的轉(zhuǎn)折處,有的人選擇早早結(jié)婚生子,雖然可能日子過(guò)的雞飛狗跳,但在我們眼里也是其樂(lè)融融, 畢...
    飄i閱讀 340評(píng)論 2 0
  • 遇見(jiàn)你那天,我們手拉手散步在花園,吃著糖果,聞著花香,聽(tīng)著鳥(niǎo)叫,看著溪水,甜甜地滋味,風(fēng)情有萬(wàn)種。 你離開(kāi)那天,頭...
    CalmEsae閱讀 602評(píng)論 8 3
  • (1)document.getElementById("myHead"); (2)document.getElem...
    LOOK_LOOK閱讀 481評(píng)論 0 0

友情鏈接更多精彩內(nèi)容