本文作者:小愛
原文鏈接:http://t.cn/RW19bxX
簡介
本文主要介紹項(xiàng)目中常用的一些庫,都是目前流行的,使用量大的庫。后期再對(duì)其中的一些庫,如Rxjava、RxAndroid、retrofit、androidannotations、react-native,做細(xì)節(jié)的分析,到時(shí)候再附上使用的demo。
Rx系列
ReactiveX是ReactiveExtensions的縮寫,簡寫為Rx;Rx是一個(gè)編程模型,目標(biāo)是提供一致的編程接口,幫助開發(fā)者更方便的處理異步數(shù)據(jù)流;Rx庫支持 .NET、JavaScript和C++,java;RxJava就是對(duì)java語言的支持。
Rx相關(guān)介紹:http://t.cn/RW1WPt1
·RxJava:
觀察者模式、響應(yīng)式編程、函數(shù)式風(fēng)格、簡化代碼,更輕松的使用并發(fā),開發(fā)必備神器。
1.github源碼:http://t.cn/RqwDy4I
2.官方文檔:http://t.cn/RWBcd7Z
Rxjava文檔中文版:http://t.cn/RyakcO0
rxjava使用:http://t.cn/RWBV5qW
3.Awesome-RxJava (關(guān)于rxjava相關(guān)內(nèi)容集錦):http://t.cn/RLU84J8
給 Android 開發(fā)者的 RxJava 詳解:http://t.cn/RymqrjF
rxjava相關(guān):
http://t.cn/RWBV3hD
4.android studio中引入,build.grade的dependencies中引用舉例:
dependencies {
compile 'io.reactivex:rxjava:1.0.y-SNAPSHOT'
}
·RxAndroid:
在RxJava的基礎(chǔ)上擴(kuò)展了一些Android的功能。除了下面提到的RxBinding,RxLifecycle,還有很多別的擴(kuò)展庫,有興趣的小伙伴可以自己看看, 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) {
//一般為耗時(shí)操作,網(wǎng)絡(luò)獲取數(shù)據(jù)或者讀取數(shù)據(jù)庫等
ArrayList<MyItem> localData = MyDbManager.getDbDatas();
subscriber.onNext(localData); //數(shù)據(jù)獲取之后,返回獲取的數(shù)據(jù)
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io()) //獲取數(shù)據(jù)在io線程中
.observeOn(AndroidSchedulers.mainThread()) //得到數(shù)據(jù)之后,在主線程更新界面和數(shù)據(jù)
.subscribe(new Observer<ArrayList<MyItem>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(ArrayList<MyItem> items) {
//得到數(shù)據(jù),do something
}
});
·RxBanding:
Android控件的事件綁定,處理控件的異步調(diào)用,使用非常方便。
1.github源碼:http://t.cn/RGX3HCz
2.簡單示例:
//防止多擊,500ms內(nèi)算一次點(diǎn)擊
RxView.clicks(view)
.throttleFirst(500, TimeUnit.MILLISECONDS)
.subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
//點(diǎn)擊事件處理
}
});
RxLifecycle:
綁定生命,例如,使用Retrofit請(qǐng)求網(wǎng)絡(luò)的時(shí)候,可以直接綁定生命周期,在界面退出時(shí),取消請(qǐng)求。
1.github源碼:http://t.cn/RWBCSTT
2.簡單示例
//偽代碼
Observable.compose(this.<MyData>bindToLifecycle()) //activity中
Observable..compose(this.<MyData>bindUntilEvent(FragmentEvent.DETACH)) //Fragment中
網(wǎng)絡(luò)系列
網(wǎng)絡(luò)請(qǐng)求比較流行的幾個(gè)開源庫,我們項(xiàng)目中基本都用上了,此處做一些簡單介紹。個(gè)人最喜歡retrofit,結(jié)合Rxjava,RxAndroid簡直完美。
·okhttp:
Square(http://t.cn/zHFOVgN) 門下的代表作之一,聽說從Android4.4開始HttpURLConnection的底層實(shí)現(xiàn)采用的是okHttp,支持SPDY、連接池、GZIP、HTTP 緩存。
1.github源碼:http://t.cn/RPYGAzG
2.官網(wǎng):http://t.cn/zTTEGdt
3.wiki:http://t.cn/RWBOwxL
wiki中文翻譯:http://t.cn/R5H0s2H
·retrofit:
Retrofit與okhttp共同出自于 Square(http://t.cn/zHFOVgN) ,retrofit對(duì)okhttp做了一層封裝,真正的網(wǎng)絡(luò)請(qǐng)求,默認(rèn)使用的是okhttp。結(jié)合RxJava,RxAndroid,代碼清晰明了。
1.github源碼:http://t.cn/RAxcIhG
2.官網(wǎng):http://t.cn/8sjciAJ
·volley:
2013年Google I/O大會(huì)上推出了一個(gè)網(wǎng)絡(luò)通信框架—— Volley.公司有一個(gè)項(xiàng)目中用的是這個(gè)網(wǎng)絡(luò)請(qǐng)求框架,不過發(fā)現(xiàn)一個(gè)bug,退出activity時(shí)取消網(wǎng)絡(luò)請(qǐng)求,下次進(jìn)入,可能會(huì)出現(xiàn)本次請(qǐng)求沒有走success和failure的回調(diào),是因?yàn)橹暗腸ancel引起的bug,不知道現(xiàn)在有沒有解決這個(gè)bug.
1.源碼:http://t.cn/zHrXk4u
2.下載源碼:
git clone https://android.googlesource.com/platform/frameworks/volley
圖片系列
圖片加載這塊,不管使用哪個(gè)庫或者自己寫,用起來多簡單,都建議多一次封裝,寫個(gè)ImageUtils,將所有的圖片加載放在這里面,這樣以后如果有問題,或者需要替換別的圖片庫,會(huì)方便很多,代碼也更易管理。
·Picasso
同樣是square門下的,是較輕量級(jí)圖片緩存庫,本身沒有做本地緩存,交給了網(wǎng)絡(luò)庫 okhttp 去實(shí)現(xiàn),簡單好用。
1.github源碼:http://t.cn/R7wKFyG
2.官網(wǎng):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
強(qiáng)大的圖片加載組件,支持加載Gif圖和WebP,不過感覺使用起來沒有picasso和glide那么簡單。
1.fresco官網(wǎng):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現(xiàn)在可是火到不行啊,它的宣傳語是“Learn once,write anywhere”。
1.github源碼:https://github.com/facebook/react-native
2.官方文檔:http://t.cn/RAyAbks
3.中文文檔:http://t.cn/Rbux8fi
4.極客學(xué)院文檔:http://t.cn/RAWWD2B
5.史上最詳細(xì)Windows版本搭建安裝React Native環(huán)境配置:http://t.cn/Rb1z5Hv
·LeakCanary
有時(shí)候OOM只是表象,更深層次的原因可能是內(nèi)存泄漏,什么是內(nèi)存泄漏?直白點(diǎn)說就是該內(nèi)存空間使用完之后沒有被回收,內(nèi)存泄漏嚴(yán)重會(huì)導(dǎo)致內(nèi)存很快被耗盡,從而導(dǎo)致OOM,最后程序crash.
LeakCanary可以檢測(cè)內(nèi)存泄漏,讓內(nèi)存泄漏無所遁形。使用后,在debug模式下,如果出現(xiàn)內(nèi)存泄漏,則會(huì)彈出通知,告訴你哪里出現(xiàn)了泄漏,非常好用.
1.github源碼:http://t.cn/RAeXOBa
2.eakCanary使用說明:http://t.cn/RWBeUfb
3.LeakCanary中文使用說明:http://t.cn/RKEmZtl
build.gradle 中加入引用,不同的編譯使用不同的引用.目前已經(jīng)到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;
}
}
//監(jiān)控你想要監(jiān)控的對(duì)象。以此為例:
public class BaseFragment extends RxFragment {
@Override
public void onDestroy() {
super.onDestroy();
if (getActivity() != null) {
RefWatcher refWatcher = ZYApplication.getRefWatcher(getActivity());
refWatcher.watch(this);
}
}
}
·EventBus
EventBus用于發(fā)布/訂閱事件。可以替代Intent,Handler,BroadCast在Activity,Fragment,線程等之間的消息傳遞.代碼簡潔優(yōu)雅,將發(fā)送者和接收者解耦。例如:登錄功能,登錄成功之后發(fā)送一個(gè)消息,需要刷新或關(guān)閉的界面,接受這個(gè)消息,做自己想做的事情。
1.github源碼:http://t.cn/RWBkGlI
2.簡單示例:
public class AccountEvent {
private User user;//你想要傳遞的數(shù)據(jù)
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));//發(fā)消息
}
}
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了,另一方面,代碼清晰明了,優(yōu)雅的不得了。常用的比較好的注解庫有兩個(gè),一個(gè)是androidannotations,另一個(gè)是 butterknife ,butterknife很火,是JakeWharton大神的作品,火是必須的。
但是當(dāng)時(shí)我們的項(xiàng)目中用的是androidannotations,因?yàn)閍ndroidannotations不是利用的反射技術(shù),性能相對(duì)好點(diǎn),它是在本地自動(dòng)生成一個(gè)新的類,真正執(zhí)行的是它自動(dòng)生成的這個(gè)類,而且在manifest中需要注冊(cè)的也是此MyActivity_,而不是MyActivity。
1.官網(wǎng):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() {
//初始化數(shù)據(jù)
}
@Click(R.id.finish_iv)
void finish() {
//do something
}
public void loginSuccess(){
mUserPreference.edit().hasLogin().put(true).apply();
}
}
@SharedPref(value = SharedPref.Scope.UNIQUE) //作用域:整個(gè)應(yīng)用都可以使用
public interface UserPreference {
@DefaultBoolean(false)
boolean hasLogin();
}