实用的 Android 开源库整理

在Android的开发过程中,每个开发者或多或少的都使用过第三方的开源库,使用第三方的开源库可以给开发者节省大量的精力和时间,进而更好的关注应用本身的业务逻辑。

下面列出一些开发者们非常常用的开源库。

Fresco

Fresco是非常强大的显示图像的开源库,它能够很好的处理图像的加载和显示。能够加载网络、本地数据库、本地资源中的图像,在图像加载出来之前,还能够预先设置一个预设的图像占位符,有二级缓存(内存和硬盘缓存)

dependencies {
  // your app's other dependencies
  compile 'com.facebook.fresco:fresco:1.0.1'
}
复制代码

另外Fresco还提供了一些其他的开源库支持 Gif,WebP等

dependencies {
  // If your app supports Android versions before Ice Cream Sandwich (API level 14)
  compile 'com.facebook.fresco:animated-base-support:1.0.1'
  // For animated GIF support
  compile 'com.facebook.fresco:animated-gif:1.0.1'
  // For WebP support, including animated WebP
  compile 'com.facebook.fresco:animated-webp:1.0.1'
  compile 'com.facebook.fresco:webpsupport:1.0.1'
  // For WebP support, without animations
  compile 'com.facebook.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.facebook.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.facebook.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

GreenDao是一个开源的Android ORM框架,更好的操作SQlite,提供友好的接口操作底层数据库的操作

Realm

简单快速的存储,节省更多的开发时间,是一个移动设备的数据库

Timber

Timber是一个开源的log框架

compile 'com.jakewharton.timber:timber:4.5.1'
复制代码

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);
复制代码

开源库相关视频讲解

Android中最常用的8个模块和第三方开源模块(Jetpack/IOC/RxJava/插件化/热修复设计/网络访问框架/组件化/图片加载框架

Guess you like

Origin juejin.im/post/7045932909852622856