您的位置:首页 > 移动开发 > Android开发

实用的 Android 开源库使用指南

2018-02-22 20:04 411 查看
在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机动车。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: