Android項(xiàng)目開發(fā)流行庫(kù)推薦

未命名設(shè)計(jì) (1).png

本文作者:小愛
原文鏈接:http://t.cn/RW19bxX

簡(jiǎn)介


本文主要介紹項(xiàng)目中常用的一些庫(kù),都是目前流行的,使用量大的庫(kù)。后期再對(duì)其中的一些庫(kù),如Rxjava、RxAndroid、retrofit、androidannotations、react-native,做細(xì)節(jié)的分析,到時(shí)候再附上使用的demo。

Rx系列


ReactiveX是ReactiveExtensions的縮寫,簡(jiǎn)寫為Rx;Rx是一個(gè)編程模型,目標(biāo)是提供一致的編程接口,幫助開發(fā)者更方便的處理異步數(shù)據(jù)流;Rx庫(kù)支持 .NET、JavaScript和C++,java;RxJava就是對(duì)java語(yǔ)言的支持。

Rx相關(guān)介紹:http://t.cn/RW1WPt1

·RxJava:

觀察者模式、響應(yīng)式編程、函數(shù)式風(fēng)格、簡(jiǎn)化代碼,更輕松的使用并發(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ò)展庫(kù),有興趣的小伙伴可以自己看看, wiki 里面都有:http://t.cn/RWBXtfw

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

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

3.簡(jiǎn)單示例:

Observable.create(new Observable.OnSubscribe<ArrayList<MyItem>>() {
           @Override
           public void call(Subscriber<? super ArrayList<MyItem>> subscriber) {
               //一般為耗時(shí)操作,網(wǎng)絡(luò)獲取數(shù)據(jù)或者讀取數(shù)據(jù)庫(kù)等
               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.簡(jiǎn)單示例:

//防止多擊,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.簡(jiǎn)單示例

//偽代碼
Observable.compose(this.<MyData>bindToLifecycle()) //activity中
Observable..compose(this.<MyData>bindUntilEvent(FragmentEvent.DETACH)) //Fragment中

網(wǎng)絡(luò)系列


網(wǎng)絡(luò)請(qǐng)求比較流行的幾個(gè)開源庫(kù),我們項(xiàng)目中基本都用上了,此處做一些簡(jiǎn)單介紹。個(gè)人最喜歡retrofit,結(jié)合Rxjava,RxAndroid簡(jiǎn)直完美。

·okhttp:

Square(http://t.cn/zHFOVgN) 門下的代表作之一,聽說(shuō)從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

3.wikihttp://t.cn/RWBl9vH

·volley:

2013年Google I/O大會(huì)上推出了一個(gè)網(wǎng)絡(luò)通信框架—— Volley.公司有一個(gè)項(xiàng)目中用的是這個(gè)網(wǎng)絡(luò)請(qǐng)求框架,不過(guò)發(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è)庫(kù)或者自己寫,用起來(lái)多簡(jiǎn)單,都建議多一次封裝,寫個(gè)ImageUtils,將所有的圖片加載放在這里面,這樣以后如果有問題,或者需要替換別的圖片庫(kù),會(huì)方便很多,代碼也更易管理。

·Picasso

同樣是square門下的,是較輕量級(jí)圖片緩存庫(kù),本身沒有做本地緩存,交給了網(wǎng)絡(luò)庫(kù) okhttp 去實(shí)現(xiàn),簡(jiǎn)單好用。

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

2.官網(wǎng):http://t.cn/zHrdfuP

3.簡(jiǎn)單示例

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

·glide

不僅支持圖片緩存,還支持 Gif、WebP、縮略圖、視頻。

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

2.wikihttp://t.cn/R2OPjVO

3.簡(jiǎn)單示例

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

·fresco

強(qiáng)大的圖片加載組件,支持加載Gif圖和WebP,不過(guò)感覺使用起來(lái)沒有picasso和glide那么簡(jiǎn)單。

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)在可是火到不行啊,它的宣傳語(yǔ)是“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)說(shuō)就是該內(nèi)存空間使用完之后沒有被回收,內(nèi)存泄漏嚴(yán)重會(huì)導(dǎo)致內(nèi)存很快被耗盡,從而導(dǎo)致OOM,最后程序crash.

LeakCanary可以檢測(cè)內(nèi)存泄漏,讓內(nèi)存泄漏無(wú)所遁形。使用后,在debug模式下,如果出現(xiàn)內(nèi)存泄漏,則會(huì)彈出通知,告訴你哪里出現(xiàn)了泄漏,非常好用.

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

2.eakCanary使用說(shuō)明:http://t.cn/RWBeUfb

3.LeakCanary中文使用說(shuō)明: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.簡(jiǎn)單示例:

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,線程等之間的消息傳遞.代碼簡(jiǎn)潔優(yōu)雅,將發(fā)送者和接收者解耦。例如:登錄功能,登錄成功之后發(fā)送一個(gè)消息,需要刷新或關(guān)閉的界面,接受這個(gè)消息,做自己想做的事情。

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

2.簡(jiǎn)單示例:

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)雅的不得了。常用的比較好的注解庫(kù)有兩個(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

3.wikihttp://t.cn/RWBs99u

4.簡(jiǎn)單示例

@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();
}

你項(xiàng)目開發(fā)最中意哪個(gè)庫(kù)?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容