IOC(Inversion of Control):控制反转。开发过程当中类里面须要用到不少个成员变量java
传统的写法:你要用这些成员变量的时候,那么你就new出来用 IOC的写法:你要用这些成员变量的时候,使用注解的方式自动注入进去 优势:代码量减小,加速开发 缺点:性能消耗加大,阅读性差,加速65535数组
框架例子bash
//实现Button自动findViewById的工做
@ViewById(R.id.bt_ioc)
private Button bt_ioc;
复制代码
实现思路网络
实现内容app
包含的注解介绍框架
下面的这个Activity实现了框架的全部内容ide
@ContentViewById(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
@ViewById(R.id.bt_ioc)
private Button bt_ioc;
@StringById(R.string.app_name)
private String app_name;
@ColorById(R.color.colorAccent)
private int color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//IOC演示
InjectManager.inject(this);
bt_ioc.setText(app_name);
bt_ioc.setBackgroundColor(color);
}
//支持数组形式的绑定,绑定多个控件
@OnClick({R.id.open_ioc})
@OnLongClick({R.id.open_ioc})
@CheckNet()
public void open_ioc() {
Toast.makeText(this, "网络可用", Toast.LENGTH_SHORT).show();
}
}
复制代码
框架的实现分为两步:自定义注解的建立和经过反射进行注入函数
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnClick {
int[] value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnLongClick {
int[] value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ColorById {
int value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ContentViewById {
int value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StringById {
int value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewById {
int value();
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckNet {
}
复制代码
Target注解的介绍布局
Retention注解的介绍性能
从使用中能够看到,注入中最重要的步骤的是:InjectManager.inject(this),这里主要负责的事情有
public class InjectManager {
public static void inject(Activity activity) {
inject(new ViewManager(activity), activity);
}
public static void inject(Fragment fragment) {
inject(new ViewManager(fragment), fragment);
}
/**
* 注入
*
* @param viewManager
* @param object
*/
private static void inject(ViewManager viewManager, Object object) {
InjectManagerService.injectContentView(viewManager, object);
InjectManagerService.injectField(viewManager, object);
InjectManagerService.injectEvent(viewManager, object);
}
}
复制代码
这里会使用到ViewManager辅助类,代码很简单,后面会用到
public class ViewManager {
private Activity mActivity;
private Fragment mFragment;
private View mView;
public ViewManager(Activity activity) {
this.mActivity = activity;
}
public ViewManager(View view) {
this.mView = view;
}
public ViewManager(Fragment fragment) {
this.mFragment = fragment;
}
/**
* 经过Id查询View
*
* @param resId
* @return
*/
public View findViewById(int resId) {
View view = null;
if (mActivity != null) {
view = mActivity.findViewById(resId);
}
if (mFragment != null) {
view = mFragment.getActivity().findViewById(resId);
}
if (mView != null) {
view = mView.findViewById(resId);
}
return view;
}
/**
* 设置根布局,仅限Activity
*
* @param resId
*/
public void setContentView(int resId) {
if (mActivity != null) {
mActivity.setContentView(resId);
}
}
/**
* 获取颜色
*
* @param resId
*/
public int getColor(int resId) {
int color = -1;
if (mActivity != null) {
color = mActivity.getResources().getColor(resId);
}
if (mFragment != null) {
color = mFragment.getActivity().getResources().getColor(resId);
}
return color;
}
/**
* 获取字符串
*
* @param resId
*/
public String getString(int resId) {
String str = "";
if (mActivity != null) {
str = mActivity.getString(resId);
}
if (mFragment != null) {
str = mFragment.getActivity().getString(resId);
}
return str;
}
}
复制代码
在InjectManagerService中,也是上面的三个主要步骤,主要仍是下面经过反射实现其真正的效果
public class InjectManagerService {
/**
* 注入根布局
*
* @param viewManager
* @param object
*/
public static void injectContentView(ViewManager viewManager, Object object) {
injectContentViewById(viewManager, object);
}
/**
* 注入变量
*
* @param viewManager
* @param object
*/
public static void injectField(ViewManager viewManager, Object object) {
injectFieldById(viewManager, object);
}
/**
* 注入事件
*
* @param viewManager
* @param object
*/
public static void injectEvent(ViewManager viewManager, Object object) {
injectOnClick(viewManager, object);
injectOnLongClick(viewManager, object);
}
/**
* 注入根布局
*
* @param viewManager
* @param object
*/
private static void injectContentViewById(ViewManager viewManager, Object object) {
Class<?> clazz = object.getClass();
ContentViewById contentView = clazz.getAnnotation(ContentViewById.class);
if (contentView != null) {
int layoutId = contentView.value();
viewManager.setContentView(layoutId);
}
}
/**
* 注入findViewById事件
*
* @param viewManager
* @param object
*/
public static void injectFieldById(ViewManager viewManager, Object object) {
//1. 获取Activity字节码,这里以Activity为例
Class<?> clazz = object.getClass();
//2. 获取字节码中全部的成员变量
Field[] fields = clazz.getDeclaredFields();
if (fields != null) {
//3. 遍历全部变量
for (Field field : fields) {
//4. 找到对应的注解
ViewById viewById = field.getAnnotation(ViewById.class);
StringById stringById = field.getAnnotation(StringById.class);
ColorById colorById = field.getAnnotation(ColorById.class);
if (viewById != null) {
//5. 获取注解中的值
int viewId = viewById.value();
//6. findViewById并设置访问权限
View view = viewManager.findViewById(viewId);
field.setAccessible(true);
try {
//7. 动态注入到变量中
field.set(object, view);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (stringById != null) {
int viewId = stringById.value();
String string = viewManager.getString(viewId);
field.setAccessible(true);
try {
field.set(object, string);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (colorById != null) {
int viewId = colorById.value();
int color = viewManager.getColor(viewId);
field.setAccessible(true);
try {
field.set(object, color);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 注入点击事件
*
* @param viewManager
* @param object
*/
public static void injectOnClick(ViewManager viewManager, Object object) {
Class<?> clazz = object.getClass();
Method[] methods = clazz.getDeclaredMethods();
if (methods != null) {
for (Method method : methods) {
OnClick onClick = method.getAnnotation(OnClick.class);
if (onClick != null) {
int[] viewIds = onClick.value();
for (int viewId : viewIds) {
View view = viewManager.findViewById(viewId);
//检查网络
boolean isCheckNet = method.getAnnotation(CheckNet.class) != null;
if (view != null) {
view.setOnClickListener(new DeclaredOnClickListener(method, object, isCheckNet));
}
}
}
}
}
}
/**
* 注入长按事件
*
* @param viewManager
* @param object
*/
public static void injectOnLongClick(ViewManager viewManager, Object object) {
Class<?> clazz = object.getClass();
Method[] methods = clazz.getDeclaredMethods();
if (methods != null) {
for (Method method : methods) {
OnLongClick onLongClick = method.getAnnotation(OnLongClick.class);
if (onLongClick != null) {
int[] viewIds = onLongClick.value();
for (int viewId : viewIds) {
View view = viewManager.findViewById(viewId);
//检查网络
boolean isCheckNet = method.getAnnotation(CheckNet.class) != null;
if (view != null) {
view.setOnLongClickListener(new DeclaredOnLongClickListener(method, object, isCheckNet));
}
}
}
}
}
}
}
复制代码
这里用到两个点击事件,而且将检查网络做为参数传进去到事件中处理,因为长按事件和点击事件大同小异,这里只贴一处代码
public class DeclaredOnLongClickListener implements View.OnLongClickListener {
private Method mMethod;
private Object mObject;
private boolean mIsCheckNet;
public DeclaredOnLongClickListener(Method method, Object object, boolean isCheckNet) {
this.mMethod = method;
this.mObject = object;
this.mIsCheckNet = isCheckNet;
}
@Override
public boolean onLongClick(View v) {
if (mIsCheckNet) {
if (!NetUtils.isNetworkAvailable(v.getContext())) {
Toast.makeText(v.getContext(), "网络不可用", Toast.LENGTH_SHORT).show();
return true;
}
}
//执行点击事件
try {
mMethod.setAccessible(true);
mMethod.invoke(mObject, v);
} catch (Exception e) {
e.printStackTrace();
try {
mMethod.invoke(mObject, null);
} catch (Exception e1) {
e1.printStackTrace();
}
}
return true;
}
}
复制代码
到这里IOC框架就结束了,其中比较重要的两点是注解的自定义和经过反射获取属性值并注入,其实代码挺简单的,反复看看仍是挺容易理解的,你们能够结合源码进行阅读,其实在IOC路上还有权限的申请等功能能够实现,不过已经有第三方框架已经作好了.