Android注解学习(2)
最近考试周没什么时间写,回归正题。前面的一次简单的讲了关于注解的的基础部分,这一次分析xutils注解封装的源码(奉上github源码)。
补充下:xUtils 2.x对Android 6.0兼容不是很好, 请尽快升级至xUtils3。(如网络部分:android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类))
(1) 注解模块目录结构



(2) 分析源代码
1.元注解--annoation文件夹下三个注解
ContentView:对于activity设置布局文件
/*
* Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.xutils.view.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.TYPE)//使用范围--类,接口,枚举,Annotation类型
@Retention(RetentionPolicy.RUNTIME)//有效范围--运行时有效
public @interface ContentView {
int value();//布局资源
}
Event:
package org.xutils.view.annotation; import android.view.View; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* 事件注解.
* 被注解的方法必须具备一下形式:
* 1. private 修饰
* 2. 返回值类型没有要求
* 3. 方法名以Click或Event结尾, 否则可能被混淆编译时删除. (方法签名形式为void *(android.view.View)也可以)
* 4. 参数签名和type的接口要求的参数签名一致.
* Author: wyouflf
* Date: 13-9-9
* Time: 下午12:43
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Event { /**
* 控件的id集合, id小于1时不执行ui事件绑定.
*
* @return
*/
int[] value(); /**
* 控件的parent控件的id集合, 组合为(value[i], parentId[i] or 0).
*
* @return
*/
int[] parentId() default 0; /**
* 事件的listener, 默认为点击事件.
*
* @return
*/
Class<?> type() default View.OnClickListener.class; /**
* 事件的setter方法名, 默认为set+type#simpleName.
*
* @return
*/
String setter() default ""; /**
* 如果type的接口类型提供多个方法, 需要使用此参数指定方法名.
*
* @return
*/
String method() default "";
}
ViewInject:绑定控件id
/*
* Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.xutils.view.annotation; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target(ElementType.FIELD)//字段
@Retention(RetentionPolicy.RUNTIME)//运行时有效
public @interface ViewInject { int value();//绑定控件id /* parent view id */
int parentId() default 0;//绑定控件父布局id 默认是取0
}
2.View文件夹下4个类
ViewInfo--1.控件注解生成ViewInfo;
2.对于散列集合包括HashSet、HashMap以及HashTable通过先比较hashcode相等再比较equals来提高效率
package org.xutils.view; /**
* 比较两个ViewInfo是否相等
* Author: wyouflf
* Date: 13-12-5
* Time: 下午11:25
*/
/*package*/ final class ViewInfo {
public Object value;
public int parentId; @Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ViewInfo)) return false; ViewInfo that = (ViewInfo) o; if (parentId != that.parentId) return false;
if (value == null) return (null == that.value); return value.equals(that.value);
} @Override
public int hashCode() {
int result = value.hashCode();
result = 31 * result + parentId;
return result;
}
}
ViewInjector--定义注解绑定的接口不再详细贴了
ViewInjectorImpl(重点)--实现注解绑定接口的实现类(包括控件绑定,布局资源绑定,控件的事件绑定)
使用单例模式双重检查锁定创建ViewInjectorImpl对象并保证线程安全
简单的做了ViewInjectorImpl类的分析:
错误之处还请指出-_-。。。
Android注解学习(2)的更多相关文章
- Android注解学习(1)
对于注解这个概念刚开始不是很理解,翻阅了其他人博客,参考实现的例子开始理解与运用.以前刚开始的写android项目时,一般找定义控件并初始化控件都是调用findviewbyId,然而当一个布局页面(类 ...
- Android注解使用之通过annotationProcessor注解生成代码实现自己的ButterKnife框架
前言: Annotation注解在Android的开发中的使用越来越普遍,例如EventBus.ButterKnife.Dagger2等,之前使用注解的时候需要利用反射机制势必影响到运行效率及性能,直 ...
- Android注解使用之使用Support Annotations注解优化代码
前言: 前面学习总结了Java注解的使用,博客地址详见Java学习之注解Annotation实现原理,从本质上了解到什么注解,以及注解怎么使用?不要看见使用注解就想到反射会影响性能之类,今天我们就来学 ...
- Android:学习AIDL,这一篇文章就够了(下)
前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...
- android注解入门 并来自己写一个框架
介绍 这里我带大家来学习一下注解 并且用来写下一个模仿xUtils3 中View框架 此框架 可以省略activity或者fragment的 findViewById 或者设置点击事件的烦恼 我正参加 ...
- 开发自己的山寨Android注解框架
目录 开发自己的山寨Android注解框架 开发自己的山寨Android注解框架 参考 Github黄油刀 Overview 在上一章我们学习了Java的注解(Annotation),但是我想大家可能 ...
- (转载)Android:学习AIDL,这一篇文章就够了(下)
前言 上一篇博文介绍了关于AIDL是什么,为什么我们需要AIDL,AIDL的语法以及如何使用AIDL等方面的知识,这一篇博文将顺着上一篇的思路往下走,接着介绍关于AIDL的一些更加深入的知识.强烈建议 ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- Android开发学习路线图
Android开发学习方法: Android是一个比较庞大的体系,从底层的Linux内核到上层的应用层,各部分的内容跨度也比较大.因此,一个好的学习方法对我们学习Android开发很重要. 在此建议, ...
随机推荐
- Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- 总结分享十大iOS开发者最喜爱的库 分类: ios相关 app相关 2015-04-03 16:43 320人阅读 评论(0) 收藏
该10大iOS开发者最喜爱的库由"iOS辅导团队"成员Marcelo Fabri组织投票选举而得,参与者包括开发者团队,iOS辅导团队以及行业嘉宾.每个团队都要根据以下规则选出五个 ...
- vi的基本操作
vi的基本操作 a) 进入vi 在系统提示符号输入vi及文件名称后,就进入vi全屏幕编辑画面: $ vi myfile 不过有一点要特别注意,就是您进入vi之后,是处于「命令行模式(command m ...
- 3)Javascript设计模式:Observer模式
Observer模式 var Observer = (function() { var instance = null; function Observe() { this.events = {} } ...
- AtCoder Beginner Contest 052 ABCD题
A - Two Rectangles Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement The ...
- Java解析JSON文件的方法(一)
一.首先需要在Eclipse工程中导入相关的jar包,jar包参见链接:http://yunpan.alibaba-inc.com/share/link/NdA5b6IFK 二.提供一份待解析的jso ...
- Bagging和Boosting 概念及区别
Bagging和Boosting都是将已有的分类或回归算法通过一定方式组合起来,形成一个性能更加强大的分类器,更准确的说这是一种分类算法的组装方法.即将弱分类器组装成强分类器的方法. 首先介绍Boot ...
- imageX批量安裝windows7
以一臺電腦爲sample,安裝完成windows os及其全部update,安裝全部常用軟件,打印機驅動,網絡磁碟機等. 關閉安全控制"UAC"和"維護解決方案" ...
- shell 远程备份日志
#!/bin/bash #Function:自动备份给定列表中的目录或文件,并且可以保留N天备份的档案. #可备份至远程主机指定的目录下,但需本机能免密码登录到远程主机,用到ssh-keygen #该 ...
- Android性能优化(一)之启动加速35%
一.前言 随着项目版本的迭代,App的性能问题会逐渐暴露出来,而好的用户体验与性能表现紧密相关,从本篇文章开始,我将开启一个Android应用性能优化的专题,从理论到实战,从入门到深挖,手把手将性能优 ...