Android项目开发流行库推荐

Android项目开发流行库推荐_第1张图片
未命名设计 (1).png

本文作者:小爱
原文链接:http://t.cn/RW19bxX

简介


本文主要介绍项目中常用的一些库,都是目前流行的,使用量大的库。后期再对其中的一些库,如Rxjava、RxAndroid、retrofit、androidannotations、react-native,做细节的分析,到时候再附上使用的demo。

Rx系列


ReactiveX是ReactiveExtensions的缩写,简写为Rx;Rx是一个编程模型,目标是提供一致的编程接口,帮助开发者更方便的处理异步数据流;Rx库支持 .NET、JavaScript和C++,java;RxJava就是对java语言的支持。

Rx相关介绍:http://t.cn/RW1WPt1

·RxJava:

观察者模式、响应式编程、函数式风格、简化代码,更轻松的使用并发,开发必备神器。

1.github源码:http://t.cn/RqwDy4I

2.官方文档:http://t.cn/RWBcd7Z
Rxjava文档中文版:http://t.cn/RyakcO0
rxjava使用:http://t.cn/RWBV5qW

3.Awesome-RxJava (关于rxjava相关内容集锦):http://t.cn/RLU84J8
给 Android 开发者的 RxJava 详解:http://t.cn/RymqrjF
rxjava相关:
http://t.cn/RWBV3hD

4.android studio中引入,build.grade的dependencies中引用举例:

dependencies {
       compile 'io.reactivex:rxjava:1.0.y-SNAPSHOT'
}

·RxAndroid:

在RxJava的基础上扩展了一些Android的功能。除了下面提到的RxBinding,RxLifecycle,还有很多别的扩展库,有兴趣的小伙伴可以自己看看, wiki 里面都有:http://t.cn/RWBXtfw

1.github源码:http://t.cn/RWBXPbA

2.使用demo:http://t.cn/RWBXKGc

3.简单示例:

Observable.create(new Observable.OnSubscribe>() {
           @Override
           public void call(Subscriber> subscriber) {
               //一般为耗时操作,网络获取数据或者读取数据库等
               ArrayList localData = MyDbManager.getDbDatas();
               subscriber.onNext(localData); //数据获取之后,返回获取的数据
               subscriber.onCompleted();
           }
       })
               .subscribeOn(Schedulers.io()) //获取数据在io线程中
               .observeOn(AndroidSchedulers.mainThread()) //得到数据之后,在主线程更新界面和数据
               .subscribe(new Observer>() {
                   @Override
                   public void onCompleted() {
                   }
                   @Override
                   public void onError(Throwable e) {
                   }
                   @Override
                   public void onNext(ArrayList items) {
                       //得到数据,do something
                   }
               });

·RxBanding:

Android控件的事件绑定,处理控件的异步调用,使用非常方便。

1.github源码:http://t.cn/RGX3HCz

2.简单示例:

//防止多击,500ms内算一次点击
RxView.clicks(view)
   .throttleFirst(500, TimeUnit.MILLISECONDS)
   .subscribe(new Action1() {
       @Override
       public void call(Void aVoid) {
           //点击事件处理
       }
   });

RxLifecycle:

绑定生命,例如,使用Retrofit请求网络的时候,可以直接绑定生命周期,在界面退出时,取消请求。

1.github源码:http://t.cn/RWBCSTT

2.简单示例

//伪代码
Observable.compose(this.bindToLifecycle()) //activity中
Observable..compose(this.bindUntilEvent(FragmentEvent.DETACH)) //Fragment中

网络系列


网络请求比较流行的几个开源库,我们项目中基本都用上了,此处做一些简单介绍。个人最喜欢retrofit,结合Rxjava,RxAndroid简直完美。

·okhttp:

Square(http://t.cn/zHFOVgN) 门下的代表作之一,听说从Android4.4开始HttpURLConnection的底层实现采用的是okHttp,支持SPDY、连接池、GZIP、HTTP 缓存。

1.github源码:http://t.cn/RPYGAzG

2.官网:http://t.cn/zTTEGdt

3.wiki:http://t.cn/RWBOwxL
wiki中文翻译:http://t.cn/R5H0s2H

·retrofit:

Retrofit与okhttp共同出自于 Square(http://t.cn/zHFOVgN) ,retrofit对okhttp做了一层封装,真正的网络请求,默认使用的是okhttp。结合RxJava,RxAndroid,代码清晰明了。

1.github源码:http://t.cn/RAxcIhG

2.官网:http://t.cn/8sjciAJ

3.wiki:http://t.cn/RWBl9vH

·volley:

2013年Google I/O大会上推出了一个网络通信框架—— Volley.公司有一个项目中用的是这个网络请求框架,不过发现一个bug,退出activity时取消网络请求,下次进入,可能会出现本次请求没有走success和failure的回调,是因为之前的cancel引起的bug,不知道现在有没有解决这个bug.

1.源码:http://t.cn/zHrXk4u

2.下载源码:

git clone https://android.googlesource.com/platform/frameworks/volley

图片系列


图片加载这块,不管使用哪个库或者自己写,用起来多简单,都建议多一次封装,写个ImageUtils,将所有的图片加载放在这里面,这样以后如果有问题,或者需要替换别的图片库,会方便很多,代码也更易管理。

·Picasso

同样是square门下的,是较轻量级图片缓存库,本身没有做本地缓存,交给了网络库 okhttp 去实现,简单好用。

1.github源码:http://t.cn/R7wKFyG

2.官网:http://t.cn/zHrdfuP

3.简单示例

Picasso.with(context).load(uri).placeholder(R.drawable.placeholder).into(view);

·glide

不仅支持图片缓存,还支持 Gif、WebP、缩略图、视频。

1.github源码:http://t.cn/RhVhUS6

2.wiki:http://t.cn/R2OPjVO

3.简单示例

Glide.with(context).load(uri).placeholder(R.drawable.placeholder).into(view);

·fresco

强大的图片加载组件,支持加载Gif图和WebP,不过感觉使用起来没有picasso和glide那么简单。

1.fresco官网:http://www.fresco-cn.org/

2.github源码:http://t.cn/RAytkHM

3.fresco demo:http://t.cn/RWBmOGR

4.fresco的使用:http://t.cn/RWBulH4

其他


·react-native

react-native现在可是火到不行啊,它的宣传语是“Learn once,write anywhere”。

1.github源码:https://github.com/facebook/react-native

2.官方文档:http://t.cn/RAyAbks

3.中文文档:http://t.cn/Rbux8fi

4.极客学院文档:http://t.cn/RAWWD2B

5.史上最详细Windows版本搭建安装React Native环境配置:http://t.cn/Rb1z5Hv

·LeakCanary

有时候OOM只是表象,更深层次的原因可能是内存泄漏,什么是内存泄漏?直白点说就是该内存空间使用完之后没有被回收,内存泄漏严重会导致内存很快被耗尽,从而导致OOM,最后程序crash.

LeakCanary可以检测内存泄漏,让内存泄漏无所遁形。使用后,在debug模式下,如果出现内存泄漏,则会弹出通知,告诉你哪里出现了泄漏,非常好用.

1.github源码:http://t.cn/RAeXOBa

2.eakCanary使用说明:http://t.cn/RWBeUfb

3.LeakCanary中文使用说明:http://t.cn/RKEmZtl

build.gradle 中加入引用,不同的编译使用不同的引用.目前已经到1.4版本了,具体见 github

dependencies {
   debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
  forTestCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'
   releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1'
}

5.简单示例:

public class MyApplication extends MultiDexApplication {
   private RefWatcher mRefWatcher;
   @Override
   public void onCreate() {
       super.onCreate();
        // init memory leak detection
       mRefWatcher = LeakCanary.install(this);
   }
  public static RefWatcher getRefWatcher(Context context) {
   MyApplication application = (MyApplication) context.getApplicationContext();
       return application.mRefWatcher;
   }
}
//监控你想要监控的对象。以此为例:
public class BaseFragment extends RxFragment {
   @Override
   public void onDestroy() {
       super.onDestroy();
       if (getActivity() != null) {
   RefWatcher refWatcher = ZYApplication.getRefWatcher(getActivity());
           refWatcher.watch(this);
       }
   }
}

·EventBus

EventBus用于发布/订阅事件。可以替代Intent,Handler,BroadCast在Activity,Fragment,线程等之间的消息传递.代码简洁优雅,将发送者和接收者解耦。例如:登录功能,登录成功之后发送一个消息,需要刷新或关闭的界面,接受这个消息,做自己想做的事情。

1.github源码:http://t.cn/RWBkGlI

2.简单示例:

public class AccountEvent {
   private User user;//你想要传递的数据
   public AccountEvent(User user) {
       this.user = user;
   }
   public User getUser() {
       return user;
   }
   public void setUser(User user) {
       this.user = user;
   }
}
public class LoginActivity {
   public loginSuccess(User user) {
  EventBus.getDefault().post(new AccountEvent(user));//发消息
   }
}
public class MyFragment{
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       EventBus.getDefault().register(this);
   }
   @Override
   public void onDestroy() {
       super.onDestroy();
       EventBus.getDefault().unregister(this);
   }
   public void onEvent(AccountEvent event) {//接受消息
       //do something
   }
}

·androidannotations

注解,一方面可以减少代码量,再也不用findViewById了,另一方面,代码清晰明了,优雅的不得了。常用的比较好的注解库有两个,一个是androidannotations,另一个是 butterknife ,butterknife很火,是JakeWharton大神的作品,火是必须的。

但是当时我们的项目中用的是androidannotations,因为androidannotations不是利用的反射技术,性能相对好点,它是在本地自动生成一个新的类,真正执行的是它自动生成的这个类,而且在manifest中需要注册的也是此MyActivity_,而不是MyActivity。

1.官网:http://t.cn/zjarUAs

2.github源码:http://t.cn/RWBs63k

3.wiki :http://t.cn/RWBs99u

4.简单示例

@EActivity(R.layout.activity_my)
public class MyActivity extends BaseActivity {
   @StringRes(R.string.my_string)
   String mMyString;
   @ViewById(R.id.tv)
   TextView mTV;
   @Extra()
   int mCount;
   @Pref
   UserPreference_ mUserPreference;
   @AfterViews
   void initialize() {
   //初始化数据
   }
   @Click(R.id.finish_iv)
   void finish() {
       //do something
   }
   public void loginSuccess(){
 mUserPreference.edit().hasLogin().put(true).apply();
   }
}
@SharedPref(value = SharedPref.Scope.UNIQUE) //作用域:整个应用都可以使用
public interface UserPreference {
   @DefaultBoolean(false)
   boolean hasLogin();
}

你项目开发最中意哪个库?

你可能感兴趣的:(Android项目开发流行库推荐)