《Android进阶之光》--注解与依赖注入框架

No1:

标准注解:

1)@Override:覆写

2)@Deprecated:过时

3)@SuppressWarnings:取消警告

4)@SafeVarargs:申明使用了可变长度参数的方法

No2:

元注解:用来注解其他注解,从而创建新的注解

1)@Targe:注解所修饰的对象范围

2)@Inherited:注解可以被继承

3)@Documented:应该被JavaDoc工具记录

4)@Retention:保留策略

5)@Repeatable:一个注解在同一声明类型上多次使用

No3:

定义注解:

1)基本定义

public @interface Swordsman{
    
}
//使用
@Swordsman
public class AnnotationTest{
    
}

2)定义成员变量

public @interface Swordsman{
    String name();
    int age();
}
//使用
public class AnnotationTest{
    @Swordsman(name="张无忌",age=23)
    public void fighting(){
        ...
    }
}
public @interface Swordsman{
    String name() default "张无忌";
    int age() default 23;
}
//使用
public class AnnotationTest{
    @Swordsman
    public void fighting(){
        
    }
}

3)定义运行时注解

@Retention设定注解的保留策略

RetentionPolicy.RUNTIME:运行时动态获取注解信息

RententionPolicy.CLASS:编译时进行一些预处理操作

RententionPolicy.SOURCE:做一些检查性的操作

@Rentation(RetentationPolicy.CLASS)
public @interface Swordsman{
    String name() default "张无忌";
    int age() default 23;
}

No4:

运行时注解处理器

public class AnnotationProcessor{
    public static void main(String[] args){
        Method[] methods = AnnotationTest.class.getDeclaredMethods();
        for(Method m:methods){
            GET get = m.getAnnotation(GET.class);
            System.out.println(get.value());
        }
    }
}

No5:

编译时注解处理器:

public class ClassProcessor extends AbstractProcessor{
    @Override
    public synchronized void init(ProcessingEnvironment processingEnv){
        super.init(processingEnv);
    }
    @Override
    public boolean process(Set<? extends TypeElement> annotations,RoundEnvironment roundEnv){
        Messager messager = processingEnv.getMessager();
        for(Element element:roundEnv.getElementsAnnotatedWith(BindView.class)){
            if(element.getKind()==ElementKind.FIELD){
                messager.printMessage(Diagnostic.Kind.NOTE,"printMessage:"+element.toString());
            }
        }
        return true;
    }
    @Override
    public Set<String> getSupportedAnnotationTypes(){
        Set<String> annotations = new LinkedHashSet<String>();
        annotations.add(BindView.class.getCanonicalName());
        return annotations;
    }
    @Override
    public SourceVersion getSupportedSourceVersion(){
        return SourceVersion.latestSupported();
    }
}

No6:

依赖注入:IoC(控制反转)容器在运行期间,动态地将某种依赖关系注入到对象中

No7:

为了解耦

public class Car{
    private Engine mEngine;
    public Car(){
        mEngine = new PetrolEngine();
    }
}

1)构造方法注入

public class Car{
    private Engine mEngine;
    public Car(Engine mEngine){
        this.mEngine = mEngine;
    }
}

2)Setter方法注入

public class Car{
    private Engine mEngine;
    public void set(Engine mEngine){
        this.mEngine = mEngine;
    }
}

3)接口注入

public interface ICar{
    public void setEngine(Engine engine);
}

public class Car implements ICar{
    private Engine mEngine;
    @Override
    public void setEngine(Engine engine){
        this.mEngine = engine;
    }
}

No8:

《Android进阶之光》--ButterKnife

No9:

《Android进阶之光》--Dagger2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书是一本Android进阶类书籍,采用理论、源码和实践相结合的方式来阐述高水准的Android应用开发要点。本书从三个方面来组织内容。第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android代码和应用层开发过程,融会贯通,介绍一些比较深入的知识点;第三,介绍一些核心技术和Android的性能优化思想。 第1章 Activity的生命周期和启动模式 1 1.1 Activity的生命周期全面分析 1 1.1.1 典型情况下的生命周期分析 2 1.1.2 异常情况下的生命周期分析 8 1.2 Activity的启动模式 16 1.2.1 Activity的LaunchMode 16 1.2.2 Activity的Flags 27 1.3 IntentFilter的匹配规则 28 第2章 IPC机制 35 2.1 Android IPC简介 35 2.2 Android中的多进程模式 36 2.2.1 开启多进程模式 36 2.2.2 多进程模式的运行机制 39 2.3 IPC基础概念介绍 42 2.3.1 Serializable接口 42 2.3.2 Parcelable接口 45 2.3.3 Binder 47 2.4 Android中的IPC方式 61 2.4.1 使用Bundle 61 2.4.2 使用文件共享 62 2.4.3 使用Messenger 65 2.4.4 使用AIDL 71 2.4.5 使用ContentProvider 91 2.4.6 使用Socket 103 2.5 Binder连接池 112 2.6 选用合适的IPC方式 121 第3章 View的事件体系 122 3.1 View基础知识 122 3.1.1 什么是View 123 3.1.2 View的位置参数 123 3.1.3 MotionEvent和TouchSlop 125 3.1.4 VelocityTracker、GestureDetector和Scroller 126 3.2 View的滑动 129 3.2.1 使用scrollTo/scrollBy 129 3.2.2 使用动画 131 3.2.3 改变布局参数 133 3.2.4 各种滑动方式的对比 133 3.3 弹性滑动 135 3.3.1 使用Scroller 136 3.3.2 通过动画 138 3.3.3 使用延时策略 139 3.4 View的事件分发机制 140 3.4.1 点击事件的传递规则 140 3.4.2 事件分发的源码解析 144 3.5 View的滑动冲突 154 3.5.1 常见的滑动冲突场景 155 3.5.2 滑动冲突的处理规则 156 3.5.3 滑动冲突的解决方式 157 第4章 View的工作原理 174 4.1 初识ViewRoot和DecorView 174 4.2 理解MeasureSpec 177 4.2.1 MeasureSpec 177 4.2.2 MeasureSpec和LayoutParams的对应关系 178 4.3 View的工作流程 183 4.3.1 measure过程 183 4.3.2 layout过程 193 4.3.3 draw过程 197 4.4 自定义View 199 4.4.1 自定义View的分类 200 4.4.2 自定义View须知 201 4.4.3 自定义View示例 202 4.4.4 自定义View的思想 217 第5章 理解RemoteViews 218 5.1 RemoteViews的应用 218 5.1.1 RemoteViews在通知栏上的应用 219 5.1.2 RemoteViews在桌面小部件上的应用 221 5.1.3 PendingIntent概述 228 5.2 RemoteViews的内部机制 230 5.3 RemoteViews的意义 239 第6章 Android的Drawable 243 6.1 Drawable简介 243 6.2 Drawable的分类 244 6.2.1 BitmapDrawable 244 6.2.2 ShapeDrawable 247 6.2.3 LayerDrawable 251 6.2.4 StateListDrawable 253 6.2.5 LevelListDrawable 255 6.2.6 TransitionDrawable 256 6.2.7 Ins
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值