1,昨天在使用RxJava的时候,调用map、filter之类的方法要创建挺多的匿名内部类,所以我们打算试用一下Lambda让我们的代码更有阅读新性,下看一下我们的对比
在使用之前我们代码是这样的
Observable.from(folders) .flatMap(new Func1>() { @Override public Observable call(File file) { return Observable.from(file.listFiles()); } }) .filter(new Func1 () { @Override public Boolean call(File file) { return file.getName().endsWith(".png"); } }) .map(new Func1 () { @Override public Bitmap call(File file) { return getBitmapFromFile(file); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1 () { @Override public void call(Bitmap bitmap) { imageCollectorView.addImage(bitmap); } });
使用之后
Observable.from(folders) .flatMap((Func1) (folder) -> { Observable.from(file.listFiles()) }) .filter((Func1) (file) -> { file.getName().endsWith(".png") }) .map((Func1) (file) -> { getBitmapFromFile(file) }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe((Action1) (bitmap) -> { imageCollectorView.addImage(bitmap) });
有没有很简洁,来看一下它在Android studio怎么使用吧,首先这是官网地址:https://github.com/evant/gradle-retrolambda,可以按照它这样一步步集成
①,JDK至少是1.8的(还是1.6或者1.7的同学去重新装一下)
②,在工程的build.gradle文件中加入下面这句话:
classpath 'me.tatarka:gradle-retrolambda:3.3.1'
再把工程的build.gradle全部贴出来看看
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'me.tatarka:gradle-retrolambda:3.3.1' //主要是这一句 // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
再看看App的Build文件添加下面两句
apply plugin: 'me.tatarka.retrolambda'
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}
再贴一下App的Build.gradle全部代码
apply plugin: 'com.android.application'apply plugin: 'com.neenbedankt.android-apt'apply plugin: 'me.tatarka.retrolambda'android { compileSdkVersion 25 buildToolsVersion "25.0.0" defaultConfig { applicationId "com.qianmo.myview2" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:design:25.+' compile 'com.android.support:recyclerview-v7:25.+' compile 'com.android.support:cardview-v7:25.+' compile 'com.balysv:material-ripple:1.0.2' compile 'com.jakewharton:butterknife:8.2.1' apt 'com.jakewharton:butterknife-compiler:8.2.1' compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' compile 'com.google.code.gson:gson:2.6.2'}
这样我们就可以使用了 很简单有没有!!!!!