实用的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'

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的解析和封装框架

原文

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,504评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,898评论 1 300
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,218评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,322评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,693评论 3 290
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,812评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,747评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,476评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,700评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,190评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,541评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,206评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,129评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,903评论 0 199
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,894评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,748评论 2 274

推荐阅读更多精彩内容