首先看下效果:
296.gifSmartSwipe框架中使用ActivityDoorBackConsumer
实现开门侧滑返回,ActivityDoorBackConsumer
的代码如下:
/**
* @author billy.qi
*/
public class ActivityDoorBackConsumer extends DoorConsumer {
protected ActivityTranslucentUtil mActivityTranslucentUtil;
protected Activity mActivity;
public ActivityDoorBackConsumer(Activity activity) {
this.mActivity = activity;
this.mActivityTranslucentUtil = new ActivityTranslucentUtil(activity);
}
@Override
public void onAttachToWrapper(SmartSwipeWrapper wrapper, SwipeHelper swipeHelper) {
super.onAttachToWrapper(wrapper, swipeHelper);
ActivityTranslucentUtil.convertWindowToTranslucent(mActivity);
}
@Override
public void onSwipeAccepted(int activePointerId, boolean settling, float initialMotionX, float initialMotionY) {
if (!mActivityTranslucentUtil.isTranslucent()) {
mActivityTranslucentUtil.convertActivityToTranslucent();
}
super.onSwipeAccepted(activePointerId, settling, initialMotionX, initialMotionY);
}
@Override
protected void onDisplayDistanceChanged(int distanceXToDisplay, int distanceYToDisplay, int dx, int dy) {
if (mActivityTranslucentUtil.isTranslucent()) {
super.onDisplayDistanceChanged(distanceXToDisplay, distanceYToDisplay, dx, dy);
}
}
@Override
public void onDetachFromWrapper() {
super.onDetachFromWrapper();
mActivityTranslucentUtil.convertActivityFromTranslucent();
}
@Override
protected void onOpened() {
super.onOpened();
if (mListeners == null || mListeners.isEmpty()) {
if (mActivity != null) {
mActivity.finish();
mActivity.overridePendingTransition(R.anim.anim_none, R.anim.anim_none);
}
}
}
@Override
protected void onClosed() {
super.onClosed();
mActivityTranslucentUtil.convertActivityFromTranslucent();
}
@Override
public int clampDistanceVertical(int distanceY, int dy) {
//resolve smooth problem while convert to transparent
if (mActivityTranslucentUtil.isTranslucent()) {
return super.clampDistanceVertical(distanceY, dy);
}
return 0;
}
@Override
public int clampDistanceHorizontal(int distanceX, int dx) {
//resolve smooth problem while convert to transparent
if (mActivityTranslucentUtil.isTranslucent()) {
return super.clampDistanceHorizontal(distanceX, dx);
}
return 0;
}
}
ActivityDoorBackConsumer
的辅助类是:
/**
* @author billy.qi
*/
@SuppressLint("PrivateApi")
public class ActivityTranslucentUtil {
private static Class mTranslucentConversionListenerClass;
private static Method mMethodConvertFromTranslucent;
private static Method mMethodConvertToTranslucent;
private static Method mMethodGetActivityOptions;
private static boolean mInitialedConvertToTranslucent;
private static boolean mInitialedConvertFromTranslucent;
private Activity mActivity;
private boolean mIsTranslucent;
public ActivityTranslucentUtil(Activity activity) {
this.mActivity = activity;
}
public static void convertWindowToTranslucent(Activity activity) {
if (activity != null) {
View contentView = activity.findViewById(android.R.id.content);
Drawable background = contentView.getBackground();
if (background == null) {
TypedArray a = activity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
int windowBg = a.getResourceId(0, 0);
a.recycle();
if (windowBg != 0) {
contentView.setBackgroundResource(windowBg);
}
}
Window window = activity.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.getDecorView().setBackgroundDrawable(null);
SmartSwipeWrapper wrapper = SmartSwipe.peekWrapperFor(activity);
if (wrapper != null) {
wrapper.setBackgroundColor(Color.TRANSPARENT);
}
}
}
private MessageQueue.IdleHandler convertActivityToTranslucentIdleHandler = new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
convertActivityToTranslucent();
return false;
}
};
private long convertTranslucentTimeStamp;
public void convertActivityToTranslucent() {
convertActivityToTranslucent(true);
}
public void convertActivityToTranslucent(final boolean retry) {
if (mIsTranslucent || mActivity == null) {
return;
}
if (convertingActivity != null) {
Looper.myQueue().addIdleHandler(convertActivityToTranslucentIdleHandler);
return;
}
convertTranslucentTimeStamp = SystemClock.elapsedRealtime();
final long callbackTimeStamp = convertTranslucentTimeStamp;
convertActivityToTranslucent(mActivity, new ActivityTranslucentUtil.TranslucentCallback() {
@Override
public void onTranslucentCallback(boolean translucent) {
if (callbackTimeStamp == convertTranslucentTimeStamp) {
if (retry && !translucent) {
convertActivityToTranslucent(false);
} else {
setTranslucent(translucent);
}
}
}
});
}
public void convertActivityFromTranslucent() {
convertTranslucentTimeStamp = SystemClock.elapsedRealtime();
convertActivityFromTranslucent(mActivity);
setTranslucent(false);
}
private void setTranslucent(boolean translucent) {
this.mIsTranslucent = translucent;
}
public boolean isTranslucent() {
return mIsTranslucent;
}
/** record the converting activity, resolve more than 1 xxUIs add onto the same activity */
private static WeakReference<Activity> convertingActivity;
/**
* Reflect call Activity.convertToTranslucent(...)
* @param activity activity
* @param callback callback
*/
public static void convertActivityToTranslucent(Activity activity, final TranslucentCallback callback) {
convertingActivity = new WeakReference<>(activity);
Object mTranslucentConversionListener = null;
try {
if (mTranslucentConversionListenerClass == null) {
Class[] clazzArray = Activity.class.getDeclaredClasses();
for (Class clazz : clazzArray) {
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
mTranslucentConversionListenerClass = clazz;
}
}
}
//resolve black flash at the beginning:
// Activity.convertToTranslucent(...) will takes tens of milliseconds
//thanks: https://github.com/Simon-Leeeeeeeee/SLWidget/blob/master/swipeback/src/main/java/cn/simonlee/widget/swipeback/SwipeBackHelper.java
if (mTranslucentConversionListenerClass != null) {
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean translucent = false;
if (args != null && args.length == 1) {
translucent = (Boolean) args[0];
}
convertCallback(callback, translucent);
return null;
}
};
mTranslucentConversionListener = Proxy.newProxyInstance(mTranslucentConversionListenerClass.getClassLoader(), new Class[]{mTranslucentConversionListenerClass}, invocationHandler);
}
if (mMethodConvertToTranslucent == null && mInitialedConvertToTranslucent) {
convertCallback(callback, false);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mMethodConvertToTranslucent == null) {
mInitialedConvertToTranslucent = true;
Method getActivityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
getActivityOptions.setAccessible(true);
mMethodGetActivityOptions = getActivityOptions;
Method method = Activity.class.getDeclaredMethod("convertToTranslucent", mTranslucentConversionListenerClass, ActivityOptions.class);
method.setAccessible(true);
mMethodConvertToTranslucent = method;
}
Object options = mMethodGetActivityOptions.invoke(activity);
mMethodConvertToTranslucent.invoke(activity, mTranslucentConversionListener, options);
} else {
if (mMethodConvertToTranslucent == null) {
mInitialedConvertToTranslucent = true;
Method method = Activity.class.getDeclaredMethod("convertToTranslucent", mTranslucentConversionListenerClass);
method.setAccessible(true);
mMethodConvertToTranslucent = method;
}
mMethodConvertToTranslucent.invoke(activity, mTranslucentConversionListener);
}
if (mTranslucentConversionListener == null) {
convertCallback(callback, false);
}
} catch (Throwable ignored) {
convertCallback(callback, false);
}
}
private static void convertCallback(TranslucentCallback callback, boolean translucent) {
if (callback != null) {
callback.onTranslucentCallback(translucent);
}
convertingActivity = null;
}
public static void convertActivityFromTranslucent(Activity activity) {
if (activity == null) {
return;
}
if (convertingActivity != null && convertingActivity.get() == activity) {
convertingActivity = null;
}
try {
if (mMethodConvertFromTranslucent == null) {
if (mInitialedConvertFromTranslucent) {
return;
}
mInitialedConvertFromTranslucent = true;
Method method = Activity.class.getDeclaredMethod("convertFromTranslucent");
method.setAccessible(true);
mMethodConvertFromTranslucent = method;
}
mMethodConvertFromTranslucent.invoke(activity);
} catch (Throwable ignored) {
}
}
public interface TranslucentCallback {
void onTranslucentCallback(boolean translucent);
}
}
代码实现如下:
SmartSwipe.wrap(this)
.addConsumer(new ActivityDoorBackConsumer(this))
.setScrimColor(0x7F000000)
.enableAllDirections();
[本章完...]
网友评论