實用的Android開源庫
在Android的開發過程中,每個開發者或多或少的都使用過第三方的開源庫,使用第三方的開源庫可以給開發者節省大量的精力和時間,進而更好的關注應用本身的業務邏輯
下面列出一些開發者們非常常用的開源庫
Fresco
Fresco是非常強大的顯示圖像的開源庫,它能夠很好的處理圖像的加載和顯示。能夠加載網絡、本地數據庫、本地資源中的圖像,在圖像加載出來之前,還能夠預先設置一個預設的圖像占位符,有二級緩存(內存和硬盤緩存)
dependencies {
// your app's other dependencies
compile 'com.非死book.fresco:fresco:1.0.1'
}
另外Fresco還提供了一些其他的開源庫支持 Gif,WebP等
dependencies {
// If your app supports Android versions before Ice Cream Sandwich (API level 14)
compile 'com.非死book.fresco:animated-base-support:1.0.1'
// For animated GIF support
compile 'com.非死book.fresco:animated-gif:1.0.1'
// For WebP support, including animated WebP
compile 'com.非死book.fresco:animated-webp:1.0.1'
compile 'com.非死book.fresco:webpsupport:1.0.1'
// For WebP support, without animations
compile 'com.非死book.fresco:webpsupport:1.0.1'
// Provide the Android support library (you might already have this or a similar dependency)
compile 'com.android.support:support-core-utils:24.2.1'
}
Glide
Glide是一個快速高效的多媒體管理和圖片加載框架,封裝了多媒體的解碼、內存和硬盤緩存,接口友好
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
OkHttp
OkHttp是一個為Android提供 HTTP+HTTP/2 的客戶端,很好的封裝了對網絡的請求連接
dependencies {
compile 'com.squareup.okhttp3:okhttp:3.6.0'
}
FastAndroidNetworking
FastAndroidNetworking是基于 OkHttp 的一個網絡引擎
dependencies {
compile 'com.amitshekhar.android:android-networking:0.4.0'
}
RxJava
RxJava-Reactive Extensions for the JVM
dependencies {
compile 'io.reactivex.rxjava2:rxjava:2.0.5'
}
package rxjava.examples;
import io.reactivex.*;
public class HelloWorld {
public static void main(String[] args) {
Flowable.just("Hello world").subscribe(System.out::println);
}
}
如果你使用的平臺還沒有支持Java 8的lambda,可以使用下面的代碼
Flowable.just("Hello world")
.subscribe(new Consumer<String>() {
@Override public void accept(String s) {
System.out.println(s);
}
);
EventBus
對Android的事件總線進行了優化,能在Activities、Fragments、Threads、Services等之間進行數據傳遞,更少的代碼更高的質量
compile 'org.greenrobot:eventbus:3.0.0'
- 定義事件
public static class MessageEvent { /* Additional fields if needed */ }
- 準備訂閱者
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
- 注冊和注銷訂閱者
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
- 發送事件
EventBus.getDefault().post(new MessageEvent());
Device Year Class
Device Year Class 是一個Android庫,提供了一些更好的方法來基于手機的硬件進行應用的修改
compile 'com.非死book.device.yearclass:yearclass:2.0.0
int year = YearClass.get(getApplicationContext());
if (year >= 2013) {
// Do advanced animation
} else if (year > 2010) {
// Do simple animation
} else {
// Phone too slow, don't do any animations
}
Network Connection Class
監聽網路連接質量的一個Android開源庫,用戶可以根據網絡的連接質量來調節應用的一些行為(加載低質量的圖片和視頻等)
compile 'com.非死book.network.connectionclass:connectionclass:1.0.1'
Android Debug Database
Android Debug Database是一個強大的開源庫,開發者通過它可以調試數據庫和 SharedPreferences ,可以直接通過瀏覽器查看數據庫和 SharedPreferences
debugCompile 'com.amitshekhar.android:debug-db:0.5.0'
LeakCanary
LeakCanary是一個檢測內存溢出的開源庫
dependencies {
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
}
public class ExampleApplication extends Application {
@Override public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
// Normal app init code...
}
}
MPAndroidChart
一個強大的制作圖表的開源庫,支持 線圖、餅狀圖、雷達圖、氣泡圖等
dependencies {
compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}
ButterKnife
ButterKnife是一個視圖的綁定工具,通過注釋生成一些相應的代碼,更簡潔的代碼
dependencies {
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
}
class ExampleActivity extends Activity {
@BindView(R.id.user) EditText username;
@BindView(R.id.pass) EditText password;
@BindString(R.string.login_error) String loginErrorMessage;
@OnClick(R.id.submit) void submit() {
// TODO call server...
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
}
Dagger
一個注入框架
dependencies {
compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
}
GreenDao是一個開源的Android ORM框架,更好的操作SQlite,提供友好的接口操作底層數據庫的操作
簡單快速的存儲,節省更多的開發時間,是一個移動設備的數據庫
Timber是一個開源的log框架
compile 'com.jakewharton.timber:timber:4.5.1'
Hugo
也是一個log框架
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'
Androig GPU Image
提供了基于 OpenGL的圖像濾鏡框架
repositories {
jcenter()
}
dependencies {
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Uri imageUri = ...;
mGPUImage = new GPUImage(this);
mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread
mGPUImage.setFilter(new GPUImageSepiaFilter());
// Later when image should be saved saved:
mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
}
沒有縮略圖
Uri imageUri = ...;
mGPUImage = new GPUImage(context);
mGPUImage.setFilter(new GPUImageSobelEdgeDetection());
mGPUImage.setImage(imageUri);
mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
GSON
JSON的解析和封裝框架
來自:http://www.jianshu.com/p/e31a92fdff35