Cool Android Apis 整理(一)

jopen 9年前發布 | 20K 次閱讀 Android Android開發 移動開發

Foreword

本文主要整理Cool Android Apis。

整理來源

  • Dan Lew的Android Tips Round-Up 系列文章。
    尤其感謝 Gracker 對上面系列文章的 翻譯和部分解釋

    </li>

  • 知乎問題:Android開發中,有哪些讓你覺得相見恨晚的方法、類或接口?

    </li> </ul> </blockquote>

    所以說嚴格來講這篇文章基本不是原創,但是我對每個Tip都加入或官方文檔或使用方法或效果之類的補充。整個來說算是 “把書讀厚” 的過程吧。

    Content

    • Activity.startActivities()

      </li> </ul>

      其實我們絕大多數的開發者可能是沒有用過這個方法的,根據我個人理解,用的到場景并不多。這個方法最直接的理解就是使用intent開啟多個Activity,我在Google的關于Activity.startActivities()文檔說明中,并沒有獲取到除了StartActivity之外更多的信息。于是我繼續扒源碼,果然在Context和ContextCompat下找到了更加詳細的說明和解釋。

      Context,以及 documentation of ContextCompat

      </span>
      * Launch multiple new activities.  This is generally the same as calling

      • {@link #startActivity(Intent)} for the first Intent in the array,
      • that activity during its creation calling {@link #startActivity(Intent)}
      • for the second entry, etc. Note that unlike that approach, generally
      • none of the activities except the last in the array will be created
      • at this point, but rather will be created when the user first visits
      • them (due to pressing back from the activity on top).</pre>

        從上面兩處文檔解釋我們可以理解到的,startActiviyies會創建一個新的Task Stack,任務棧里Activity的位置基于我們傳入的Intent數組。當我們按返回鍵時,就會將棧頂的Activity移除。。其實這也是framework管理用戶開啟的activity的方式。這里結合Activity的啟動模式來理解,就簡單多了。

        至于應用場景,我目前能想到的就是點擊通知欄來開啟應用的Activities,而開啟哪些Activity以及相應順序,我們就可以用到這個方法了。

        這個我想應該大部分同學都應該用過,具體些的說明: 如果傳入的String為NULL或者Length為0的話就返回false。

        如果你對Html熟悉的話,可以很迅速通過這個方法處理一些富文本操作。比如超鏈接和圖文排版等處理。

        Example:

        linkView.setText(
        
                Html.fromHtml(
                        "<b>fromHtml:</b>  \t Click " +
                                "<a href=\"http://oakzmm.com\">here</a> " +
                                "to visit my website "
                )
        );</pre><br />
        

        這個還是直接上圖吧  Cool Android Apis 整理(一)

        有不少人在知乎提到這個知識點,說是可以代替view.getVisibility() == View.VISIBLE這樣的判斷。
        但是,但是,我做了下測試:

        editText = (EditText) findViewById(R.id.text);
         System.out.println("------------------" + editText.isShown());
         System.out.println("------------------" + (editText.getVisibility() == View.VISIBLE));

        log:

        08-05 16:47:29.822  12720-12720/com.macouen.testdemo I/System.out﹕ ------------------false
        08-05 16:47:29.822  12720-12720/com.macouen.testdemo I/System.out﹕ ------------------true

        然而Google文檔):

        public boolean isShown ()                  Added in API level 1
        Returns the visibility of this view and all of its ancestors
        Returns
        True if this view and all of its ancestors are VISIBLE

        摔!老老實實用view.getVisibility() == View.VISIBLE吧。

        有些時候我們的app需要根據不同的SDK版本進行執行不同的操作

        Example:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        
         ActionBar actionBar = getActionBar();
         actionBar.setDisplayHomeAsUpEnabled(true);
        
        }</pre>

        這個方法簡單粗暴,但是我沒用過。這個方法會將輸入的字母根據鍵盤上的映射轉換為數字。

        Translates any alphabetic letters (i.e. [A-Za-z]) in the specified phone number into the equivalent numeric digits, according to the phone keypad letter mapping described in ITU E.161 and ISO/IEC 9995-8.

        所謂的ITU E.161 標準的鍵盤就是我們常用的T9鍵盤。也就是這樣
         Cool Android Apis 整理(一)

        !!重點這個方法簡直不要太吊。。
        ArgbEvaluator.evaluate(float fraction, Object startValue, Object endValue);根據一個起始顏色值和一個結束顏色值以及一個偏移量生成一個新的顏色,分分鐘實現類似于微信底部欄滑動顏色漸變。
        這里提供另一個顏色漸變的版本

        From Google Sample SlidingTabsColors下的SlidingTabStrip.java

        /**
        • Blend {@code color1} and {@code color2} using the given ratio. *
        • @param ratio of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend,
        • 0.0 will return {@code color2}. / private static int blendColors(int color1, int color2, float ratio) { final float inverseRation = 1f - ratio; float r = (Color.red(color1) ratio) + (Color.red(color2) inverseRation); float g = (Color.green(color1) ratio) + (Color.green(color2) inverseRation); float b = (Color.blue(color1) ratio) + (Color.blue(color2) * inverseRation); return Color.rgb((int) r, (int) g, (int) b); }</pre>

          PS:這里說到ARGB,簡單的提一下關于Alpha的問題 (和這個Tip并沒有聯系)。在Google I/O 2013大會上Romain Guy在Android Graphics Performance這部分提到了use alpha with care。具體的可以參考:

          Added in API level 14

          Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

          最棒的一點是Space可以跳過Draw這個過程。

          之前見不少人提過這個方法,都是說可以順暢的取消動畫效果。文檔中是這樣說的。

          Plays the ValueAnimator in reverse. If the animation is already running, it will stop itself and play backwards from the point reached when reverse was called. If the animation is not currently running, then it will start from the end and play backwards. This behavior is only set for the current animation; future playing of the animation will use the default behavior of playing forward.

          也就是說這個方法其實是反轉動畫,如果動畫正在播放,這個方法停止動畫,并從當前點往回播。如果動畫已經播放完畢那就反過來一遍。那么也就是說 “順暢的取消動畫效果” ,是動畫還在播放的是時候來調用reverse這個方法。


          待續。

          聲明

          1. 由于互聯網數據的分享性,如果我發表的文章,來源于您的原創文章,且我沒有注明,請微博私信或者郵件macouen@gmail.com說明。
          2. 歡迎轉載,但請注明文章原始出處。

          作者:Oak_Zmm
          出處:http://oakzmm.com/

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