Android中非常cool的API

http://www.zhihu.com/question/33636939

Content

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

Context,以及 documentation of ContextCompat

* 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).

從上面兩處文檔解釋我們可以理解到的,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 "
              )
      );

這個還是直接上圖吧

blob.png

有不少人在知乎提到這個知識點,說是可以代替 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);
   }

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

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鍵盤。也就是這樣

blob.png

!!重點這個方法簡直不要太吊。。
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);
   }


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 這個方法。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章