Android 之旅:Support Annotation Library 使用详解

一、环境配置

Support Annotations library 是  Android Support Repository 的一部分,想要使用

Support Annotations library 添加至我们的工程,首先需要下载 Android Support Repository

并将 Annotations library  依赖添加至 build.gradle 文件中。

步骤如下:

1,打开 SDK Manager

Android 之旅:Support Annotation Library 使用详解_第1张图片

2,点击 SDK Tools 标签

Android 之旅:Support Annotation Library 使用详解_第2张图片

3,展开 Support Repository ,选中 Support Annotations library ,再点 OK

Android 之旅:Support Annotation Library 使用详解_第3张图片

4,根据安装向导安装相关的 packages

5,在 build.gradle 文件中添加依赖

dependencies{

compile'com.android.support:support-annotations:24.2.0'}

6,点击 Sync Now,完成配置

二、Annotations 详细介绍

1,Nullness 注解

作用于函数参数和返回值 , 标记参数或者返回值是否可以为空

注解符号标记类型

@Nullable可以为空

@Nullable不可以为空

举例:

importandroid.support.annotation.NonNull;.../** Add support for inflating the  tag. **/

@NonNull

@Override

publicViewonCreateView(Stringname,@NonNullContextcontext,

@NonNullAttributeSetattrs){...

}

2,资源类注解

我们都知道,Android 中的资源都是通过 R 文件来访问的,R 文件通过整形值来标识不同的资源,如果一个方法需要传递一个 String

类型的资源 ID,但是,如果我们传递一个 Layout 资源 ID,或者直接传一个普通的 int

值,程序编译期间不会报错,但是在运行到这段代码的时候就会报错。

注解符号标记类型

@AnimatorResandroid.R.animator

@AnimResandroid.R.anim

@ArrayResandroid.R.array

@AttrResandroid.R.attr

@BoolResandroid.R.bool

@ColorResandroid.R.color

@DimenResandroid.R.dimen

@DrawableResandroid.R.drawable

@IdResandroid.R.id

@LayoutResandroid.R.layout

@RawResandroid.R.raw

@StyleableResandroid.R.styleable

@StyleResandroid.R.style

@XmlResandroid.R.xml

@InterpolatorResandroid.R.interpolator

举例:

publicabstractvoidsetTitle(@StringResintresId){

}

3 , 线程注解

注解符号标记类型

@MainThread运行在主线程

@UiThread运行在 UI 线程

@WorkerThread运行在后台线程

@BinderThread运行在Binder线程中

@AnyThread可以运行在任意线程中

说明:在Android 系统中,主线程就是 UI 线程,所以,编译工具对待 @MainThread 和 @UiThread

是同等对待的,然后,我们在使用注解的时候应该区别使用,和UI相关的代码注解使用 @UiThread,其他和 APP 生命周期相关的代码才使用

@MainThread

4,值约束注解

注解符号标记类型

@IntRange限定 int 类型的取值范围

@FloatRange限定 float 类型的取值范围

@Size限定元素的大小或者长度

举例:

a,限定 alpha 参数的取值范围为 0 至255

publicvoidsetAlpha(@IntRange(from=0,to=255)intalpha){

}

b,限定 alpha 参数的取值范围为 0.0 至 1.0

publicvoidsetAlpha(@FloatRange(from=0.0,to=1.0)floatalpha){...}

c,限定 location 数组大小为 2

publicvoidgetLocationInWindow(@Size(2)int[]location){...

}

d,限定 location 参数的最小值为 1

int[]location=newint[3];button.getLocationOnScreen(@Size(min=1)location);

5,权限注解

检查方法调用者是否具备相应的权限

注解符号标记类型

@RequiresPermission调用所需要具备的权限

举例:

@RequiresPermission(Manifest.permission.SET_WALLPAPER)publicabstractvoidsetWallpaper(Bitmapbitmap)throwsIOException;

@RequiresPermission(allOf={

Manifest.permission.READ_EXTERNAL_STORAGE,

Manifest.permission.WRITE_EXTERNAL_STORAGE})

publicstaticfinalvoidcopyFile(Stringdest,Stringsource){...}

@RequiresPermission(anyOf={ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION})publicabstractLocationgetLastKnownLocation(Stringprovider);

参数说明:allOf  表示,必须同时具备所有权限;anyOf 表示只要获取其中一个权限就可以

6 , 类型定义注解

使用 @IntDef 和@StringDef 注解,定义一组枚举类型的注解来校验参数的类型,限定参数的取值范围

注解符号标记类型

@IntDef定义 int 类型的数据

@StringDef定义 String 类型数据

举例:

importandroid.support.annotation.IntDef;

...publicclassItem{

...publicstaticfinalintHEAD_TPYE=0;publicstaticfinalintCONTENT_TPYE=1;publicstaticfinalintFOOTER_TPYE=2;@IntDef({HEAD_TPYE,CONTENT_TPYE,FOOTER_TPYE})public@interfaceItemType{}intmType;publicvoidsetType(@ItemTypeinttype){this.mType=type;

}

...

}

7,@CheckResult注解

当调用没有正确调用方法时,提示调用者对返回值进行检查处理, 使用 suggest 编写提示内容

public@CheckResultStringtrim(Strings){

returns.trim();

}...

s.trim();// 错误的调用

s=s.trim();// 正确的的调用

Android 之旅:Support Annotation Library 使用详解_第4张图片

8,@Keep 注解

标记在混淆时不需要混淆该方法

9,@CallSuper 注解

标记子类在复写父类方法的时候,必须调用父类的该方法。

参考资料:

https://developer.android.com/studio/write/annotations.html#enum-annotations

https://developer.android.com/reference/android/support/annotation/package-summary.html

欢迎关注微信公众号:“ Android 之旅 ”,一起学习 、交流 。

Android 之旅:Support Annotation Library 使用详解_第5张图片

你可能感兴趣的:(Android 之旅:Support Annotation Library 使用详解)