Cool Android Apis 整理(一)

Foreword

本文主要整理 Cool Android Apis。

整理来源

所以说严格来讲这篇文章基本不是原创,但是我对每个Tip都加入或官方文档或使用方法或效果之类的补充。整个来说算是 “把书读厚” 的过程吧。

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 这个方法。

待续。

声明

  1. 由于互联网数据的分享性,如果我发表的文章,来源于您的原创文章,且我没有注明,请微博私信或者邮件[email protected]说明。

  2. 欢迎转载,但请注明文章原始出处。

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