本文作者:小愛
原文鏈接: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<ArrayList<MyItem>>() {
@Override
public void call(Subscriber<? super ArrayList<MyItem>> subscriber) {
//一般為耗時操作,網絡獲取數據或者讀取數據庫等
ArrayList<MyItem> localData = MyDbManager.getDbDatas();
subscriber.onNext(localData); //數據獲取之后,返回獲取的數據
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io()) //獲取數據在io線程中
.observeOn(AndroidSchedulers.mainThread()) //得到數據之后,在主線程更新界面和數據
.subscribe(new Observer<ArrayList<MyItem>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ArrayList<MyItem> items) {
//得到數據,do something
}
});
·RxBanding:
Android控件的事件綁定,處理控件的異步調用,使用非常方便。
1.github源碼:http://t.cn/RGX3HCz
2.簡單示例:
//防止多擊,500ms內算一次點擊
RxView.clicks(view)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
//點擊事件處理
}
});
RxLifecycle:
綁定生命,例如,使用Retrofit請求網絡的時候,可以直接綁定生命周期,在界面退出時,取消請求。
1.github源碼:http://t.cn/RWBCSTT
2.簡單示例
//偽代碼
Observable.compose(this.<MyData>bindToLifecycle()) //activity中
Observable..compose(this.<MyData>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
·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
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
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();
}