ScreenshotUtil实现
最近多次在项目中用到截图功能,于是封装了一个截图工具,使用MediaProjection实现截图,权限申请使用了RxActivityResult。
使用方法:
- 在Application中注册
public class MyApp extends Application {
@Override public void onCreate() {
super.onCreate();
ScreenshotUtil.register(this);
}
}
- 在on方法中传入Activity 对象,在subscribe中处理截图的结果Bitmap
ScreenshotUtil.on(this).takeScreenshot().subscribe(bitmap -> { if (bitmap == null) {
Log.e(TAG, "Bitmap is null.");
} else {
mImageView.setImageBitmap(bitmap);
}});
Github: https://github.com/MissMidou/Android-Develop-Tool
RxActivityResult 实现原理
在startActivityForResult 时,需要在onActivityResult 中处理返回结果,而使用RxActivityResult,可以不用重写onActivityResult来直接处理返回结果,代码简介清晰。
Github: https://github.com/VictorAlbertos/RxActivityResult
使用范例:
public class SampleApp extends Application {
@Override public void onCreate() {
super.onCreate();
RxActivityResult.register(this);
}
}
RxActivityResult.register(this);用于判断在顶层的Activity的生命周期,用这个Activity打开目标Activity
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
RxActivityResult.on(this).startIntent(takePhoto)
.subscribe(result -> {
Intent data = result.data();
int resultCode = result.resultCode();
// the requestCode using which the activity is started can be received here.
int requestCode = result.requestCode();
if(requestCode == YourActivity.YOUR_REQUEST_CODE)
{
// Do Something
}
if (resultCode == RESULT_OK) {
result.targetUI().showImage(data);
} else {
result.targetUI().printUserCanceled();
}
});
on()返回一个Builder 对象,Builder中创建一个PublishSubject实例,PublishSubject与普通Observable相比,Observable是订阅时立即触发订阅事件,而PublishSubject可以在任意时刻调用onNext,onComplete方法触发订阅事件,观察者在订阅之后便可以收到事件,这里在onActivityResult返回结果后,触发订阅事件
public static <T extends Activity> Builder<T> on(T activity) {
return new Builder<T>(activity);
}
public static class Builder<T> {
final Class clazz;
final PublishSubject<Result<T>> subject = PublishSubject.create();
private final boolean uiTargetActivity;
... ...
}
startIntent()调用startHolderActivity,通过打开一个HolderActivity,然后在HolderActivity中打开目标Activity,并在onActivityResult处理结果
private Observable<Result<T>> startHolderActivity(Request request, @Nullable OnPreResult onPreResult) {
OnResult onResult = uiTargetActivity ? onResultActivity() : onResultFragment();
request.setOnResult(onResult);
request.setOnPreResult(onPreResult);
HolderActivity.setRequest(request);
//getOLiveActivity找到顶层Activity,因为在Fragment中打开的时候需要Activity
activitiesLifecycle.getOLiveActivity().subscribe(new Consumer<Activity>() {
@Override
public void accept(Activity activity) throws Exception {
activity.startActivity(new Intent(activity, HolderActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
}
});
return subject;
}
HolderActivity打开目标Activity后,onActivityResult中保存结果并finish,然后onDestroy 中发射订阅事件
HolderActivity.java
@Override
protected void onDestroy() {
super.onDestroy();
if (onResult != null)
onResult.response(requestCode, resultCode, data);
}
private OnResult onResultActivity() {
return new OnResult() {
@Override
public void response(int requestCode, int resultCode, Intent data) {
if (activitiesLifecycle.getLiveActivity() == null) return;
//If true it means some other activity has been stacked as a secondary process.
//Wait until the current activity be the target activity
if (activitiesLifecycle.getLiveActivity().getClass() != clazz) {
return;
}
T activity = (T) activitiesLifecycle.getLiveActivity();
subject.onNext(new Result<>(activity, requestCode, resultCode, data));
subject.onComplete();
}
@Override
public void error(Throwable throwable) {
subject.onError(throwable);
}
};
}
网友评论