实用的Android开源库
hwpcgy
8年前
<p style="text-align:center"><img src="https://simg.open-open.com/show/3c0bfe1a3d1ba261c89554d44929e8b9.png"></p> <p>在Android的开发过程中,每个开发者或多或少的都使用过第三方的开源库,使用第三方的开源库可以给开发者节省大量的精力和时间,进而更好的关注应用本身的业务逻辑</p> <p>下面列出一些开发者们非常常用的开源库</p> <h3><a href="/misc/goto?guid=4958869680969840391" rel="nofollow,noindex">Fresco</a></h3> <p>Fresco是非常强大的显示图像的开源库,它能够很好的处理图像的加载和显示。能够加载网络、本地数据库、本地资源中的图像,在图像加载出来之前,还能够预先设置一个预设的图像占位符,有二级缓存(内存和硬盘缓存)</p> <pre> <code class="language-java">dependencies { // your app's other dependencies compile 'com.非死book.fresco:fresco:1.0.1' }</code></pre> <p>另外Fresco还提供了一些其他的开源库支持 Gif,WebP等</p> <pre> <code class="language-java">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' }</code></pre> <h3><a href="/misc/goto?guid=4958862736295244246" rel="nofollow,noindex">Glide</a></h3> <p>Glide是一个快速高效的多媒体管理和图片加载框架,封装了多媒体的解码、内存和硬盘缓存,接口友好</p> <pre> <code class="language-java">dependencies { compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.android.support:support-v4:19.1.0' }</code></pre> <h3><a href="/misc/goto?guid=4958964956476581901" rel="nofollow,noindex">OkHttp</a></h3> <p>OkHttp是一个为Android提供 HTTP+HTTP/2 的客户端,很好的封装了对网络的请求连接</p> <pre> <code class="language-java">dependencies { compile 'com.squareup.okhttp3:okhttp:3.6.0' }</code></pre> <h3><a href="/misc/goto?guid=4959716305725318233" rel="nofollow,noindex">FastAndroidNetworking</a></h3> <p>FastAndroidNetworking是基于 OkHttp 的一个网络引擎</p> <pre> <code class="language-java">dependencies { compile 'com.amitshekhar.android:android-networking:0.4.0' }</code></pre> <h3><a href="/misc/goto?guid=4958865750209621132" rel="nofollow,noindex">RxJava</a></h3> <p>RxJava-Reactive Extensions for the JVM</p> <pre> <code class="language-java">dependencies { compile 'io.reactivex.rxjava2:rxjava:2.0.5' }</code></pre> <pre> <code class="language-java">package rxjava.examples; import io.reactivex.*; public class HelloWorld { public static void main(String[] args) { Flowable.just("Hello world").subscribe(System.out::println); } }</code></pre> <p>如果你使用的平台还没有支持Java 8的lambda,可以使用下面的代码</p> <pre> <code class="language-java">Flowable.just("Hello world") .subscribe(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } );</code></pre> <h3><a href="/misc/goto?guid=4958965423850786925" rel="nofollow,noindex">EventBus</a></h3> <p>对Android的事件总线进行了优化,能在Activities、Fragments、Threads、Services等之间进行数据传递,更少的代码更高的质量</p> <pre> <code class="language-java">compile 'org.greenrobot:eventbus:3.0.0'</code></pre> <ul> <li>定义事件</li> </ul> <pre> <code class="language-java">public static class MessageEvent { /* Additional fields if needed */ }</code></pre> <ul> <li>准备订阅者</li> </ul> <pre> <code class="language-java">@Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) {/* Do something */};</code></pre> <ul> <li>注册和注销订阅者</li> </ul> <pre> <code class="language-java">@Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); }</code></pre> <ul> <li>发送事件</li> </ul> <pre> <code class="language-java">EventBus.getDefault().post(new MessageEvent());</code></pre> <h3><a href="/misc/goto?guid=4959637724435948320" rel="nofollow,noindex">Device Year Class</a></h3> <p>Device Year Class 是一个Android库,提供了一些更好的方法来基于手机的硬件进行应用的修改</p> <pre> <code class="language-java">compile 'com.非死book.device.yearclass:yearclass:2.0.0</code></pre> <pre> <code class="language-java">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 }</code></pre> <h3><a href="/misc/goto?guid=4959736179633922946" rel="nofollow,noindex">Network Connection Class</a></h3> <p>监听网路连接质量的一个Android开源库,用户可以根据网络的连接质量来调节应用的一些行为(加载低质量的图片和视频等)</p> <pre> <code class="language-java">compile 'com.非死book.network.connectionclass:connectionclass:1.0.1'</code></pre> <h3><a href="/misc/goto?guid=4959725824482477240" rel="nofollow,noindex">Android Debug Database</a></h3> <p>Android Debug Database是一个强大的开源库,开发者通过它可以调试数据库和 SharedPreferences ,可以直接通过浏览器查看数据库和 SharedPreferences</p> <pre> <code class="language-java">debugCompile 'com.amitshekhar.android:debug-db:0.5.0'</code></pre> <h3><a href="/misc/goto?guid=4958874766191309432" rel="nofollow,noindex">LeakCanary</a></h3> <p>LeakCanary是一个检测内存溢出的开源库</p> <pre> <code class="language-java">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' }</code></pre> <pre> <code class="language-java">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... } }</code></pre> <h3><a href="/misc/goto?guid=4958970698590750062" rel="nofollow,noindex">MPAndroidChart</a></h3> <p>一个强大的制作图表的开源库,支持 线图、饼状图、雷达图、气泡图等</p> <pre> <code class="language-java">dependencies { compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' }</code></pre> <h3><a href="/misc/goto?guid=4958961775931067718" rel="nofollow,noindex">ButterKnife</a></h3> <p>ButterKnife是一个视图的绑定工具,通过注释生成一些相应的代码,更简洁的代码</p> <pre> <code class="language-java">dependencies { compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' }</code></pre> <pre> <code class="language-java">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... } }</code></pre> <h3><a href="/misc/goto?guid=4958870694214783028" rel="nofollow,noindex">Dagger</a></h3> <p>一个注入框架</p> <pre> <code class="language-java">dependencies { compile 'com.google.dagger:dagger:2.x' annotationProcessor 'com.google.dagger:dagger-compiler:2.x' }</code></pre> <p><a href="/misc/goto?guid=4959736179869943720" rel="nofollow,noindex">例子</a></p> <p><a href="/misc/goto?guid=4959673067565349529" rel="nofollow,noindex">GreenDao</a></p> <p>GreenDao是一个开源的Android ORM框架,更好的操作SQlite,提供友好的接口操作底层数据库的操作</p> <p><a href="/misc/goto?guid=4959673067565349529" rel="nofollow,noindex">具体的使用规则</a></p> <p><a href="/misc/goto?guid=4958973331914205897" rel="nofollow,noindex">Realm</a></p> <p>简单快速的存储,节省更多的开发时间,是一个移动设备的数据库</p> <p><a href="/misc/goto?guid=4958973331914205897" rel="nofollow,noindex">官网</a></p> <p><a href="/misc/goto?guid=4958964955571283452" rel="nofollow,noindex">Timber</a></p> <p>Timber是一个开源的log框架</p> <pre> <code class="language-java">compile 'com.jakewharton.timber:timber:4.5.1'</code></pre> <h3><a href="/misc/goto?guid=4958866344171193400" rel="nofollow,noindex">Hugo</a></h3> <p>也是一个log框架</p> <pre> <code class="language-java">buildscript { repositories { mavenCentral() } dependencies { classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1' } } apply plugin: 'com.android.application' apply plugin: 'com.jakewharton.hugo'</code></pre> <h3><a href="/misc/goto?guid=4959555795675247736" rel="nofollow,noindex">Androig GPU Image</a></h3> <p>提供了基于 OpenGL的图像滤镜框架</p> <pre> <code class="language-java">repositories { jcenter() } dependencies { compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1' }</code></pre> <pre> <code class="language-java">@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); }</code></pre> <p>没有缩略图</p> <pre> <code class="language-java">Uri imageUri = ...; mGPUImage = new GPUImage(context); mGPUImage.setFilter(new GPUImageSobelEdgeDetection()); mGPUImage.setImage(imageUri); mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);</code></pre> <h3><a href="/misc/goto?guid=4958964956010997959" rel="nofollow,noindex">GSON</a></h3> <p>JSON的解析和封装框架</p> <p> </p> <p> </p> <p>来自:http://www.jianshu.com/p/e31a92fdff35</p> <p> </p>