在项目中使用 SwipeBackLayout 或 SlidingMenu 侧滑关闭Activity框架时,因为windowIsTranslucent这个属性设置为了true,致使按home键退到桌面在返回App时会出现两个问题。java
这个是比较严重的用户体验问题,特别在小米手机上会特别明显。android
以前就出现过首页透底显示桌面的状况,是由于Theme中windowIsTranslucent = true致使这个问题,经过修改windowIsTranslucent = false属性,完全解决了首页透底问题。app
一样的配方一样的味道 替换全部全部Activity Theme 将window 改成不透明,背景颜色改成透明框架
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowBackground">@color/transparent</item>
</style>
复制代码
运行后的效果图:ide
在当前App退到后台时替换Activity为非透明主题,在Activity恢复到前台被点击时替换为透明主题; 如何动态修改Activity Theme?源码分析
@Override
protected void onCreate(Bundle savedInstanceState) {
if (current_theme!= -1){
this.setTheme(current_theme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt_theme).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeTheme(GREEN_THEME);
}
});
}
public void changeTheme(int index) {
switch (index) {
case DEFAULT_THEME:
current_theme = R.style.DefaultTheme;
break;
case GREEN_THEME:
current_theme = R.style.GreenTheme;
break;
case ORANGE_THEME:
current_theme = R.style.OrangeTheme;
break;
default:
break;
}
}
protected void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
复制代码
其实设置主题必须在任何view建立以前,因此咱们不可能在activity的onCreate以后来更改主题,若是必定要作,就只能调用setTheme(),而后调用recreate(),从新建立一个activity,而且销毁上一个activity; 因此这个方案并不可行,整个界面必须销毁重建。 已知的Android theme修改方式性能
能够经过其余方式修改Activity windowIsTranslucent 属性吗?测试
查阅Activity源码,看一下他是如何变成透明的优化
/** * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from * opaque to translucent following a call to {@link #convertFromTranslucent()}. * <p> * Calling this allows the Activity behind this one to be seen again. Once all such Activities * have been redrawn {@link TranslucentConversionListener#onTranslucentConversionComplete} will * be called indicating that it is safe to make this activity translucent again. Until * {@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image * behind the frontmost Activity will be indeterminate. * <p> * This call has no effect on non-translucent activities or on activities with the * {@link android.R.attr#windowIsFloating} attribute. * * @param callback the method to call when all visible Activities behind this one have been * drawn and it is safe to make this Activity translucent again. * @param options activity options delivered to the activity below this one. The options * are retrieved using {@link #getActivityOptions}. * @return <code>true</code> if Window was opaque and will become translucent or * <code>false</code> if window was translucent and no change needed to be made. * * @see #convertFromTranslucent() * @see TranslucentConversionListener * * @hide */
@SystemApi
public boolean convertToTranslucent(TranslucentConversionListener callback, ActivityOptions options) {
boolean drawComplete;
try {
mTranslucentCallback = callback;
mChangeCanvasToTranslucent = ActivityManager.getService().convertToTranslucent(
mToken, options == null ? null : options.toBundle());
WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
drawComplete = true;
} catch (RemoteException e) {
// Make callback return as though it timed out.
mChangeCanvasToTranslucent = false;
drawComplete = false;
}
if (!mChangeCanvasToTranslucent && mTranslucentCallback != null) {
// Window is already translucent.
mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
}
return mChangeCanvasToTranslucent;
}
/** * Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} to a * fullscreen opaque Activity. * <p> * Call this whenever the background of a translucent Activity has changed to become opaque. * Doing so will allow the {@link android.view.Surface} of the Activity behind to be released. * <p> * This call has no effect on non-translucent activities or on activities with the * {@link android.R.attr#windowIsFloating} attribute. * * @see #convertToTranslucent(android.app.Activity.TranslucentConversionListener, * ActivityOptions) * @see TranslucentConversionListener * * @hide */
@SystemApi
public void convertFromTranslucent() {
try {
mTranslucentCallback = null;
if (ActivityManager.getService().convertFromTranslucent(mToken)) {
WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, true);
}
} catch (RemoteException e) {
// pass
}
}
复制代码
能够看到这个两个Api就是将Activity转化为投透明和非透明经过 ActivityManager.getService() 和 WindowManagerGlobal.getInstance().changeCanvasOpacity()修改Window透明属性;ui
因为是系统Api 并有 @hide 标注 正常是没法调用的,能够经过反射来调用; 反射调用以下:
/** * Convert a translucent themed Activity * 将Activity 改成透明 */
public static void convertActivityToTranslucent(Activity activity) {
long timeMillis = SystemClock.currentThreadTimeMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
convertActivityToTranslucentAfterL(activity);
} else {
convertActivityToTranslucentBeforeL(activity);
}
FxLog.d("convertActivity : convertActivityToTranslucent time = " + (SystemClock.currentThreadTimeMillis() - timeMillis));
}
/** * Calling the convertToTranslucent method on platforms before Android 5.0 */
public static void convertActivityToTranslucentBeforeL(Activity activity) {
try {
Class<?>[] classes = Activity.class.getDeclaredClasses();
Class<?> translucentConversionListenerClazz = null;
for (Class clazz : classes) {
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
translucentConversionListenerClazz = clazz;
}
}
Method method = Activity.class.getDeclaredMethod("convertToTranslucent",
translucentConversionListenerClazz);
method.setAccessible(true);
method.invoke(activity, new Object[] {
null
});
} catch (Throwable t) {
}
}
/** * Calling the convertToTranslucent method on platforms after Android 5.0 */
private static void convertActivityToTranslucentAfterL(Activity activity) {
try {
Method getActivityOptions = Activity.class.getDeclaredMethod("getActivityOptions");
getActivityOptions.setAccessible(true);
Object options = getActivityOptions.invoke(activity);
Class<?>[] classes = Activity.class.getDeclaredClasses();
Class<?> translucentConversionListenerClazz = null;
for (Class clazz : classes) {
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
translucentConversionListenerClazz = clazz;
}
}
Method convertToTranslucent = Activity.class.getDeclaredMethod("convertToTranslucent",
translucentConversionListenerClazz, ActivityOptions.class);
convertToTranslucent.setAccessible(true);
convertToTranslucent.invoke(activity, null, options);
} catch (Throwable t) {
}
}
复制代码
这样的反射是否对性能有损耗呢?在调用时作了耗时测试 在日志打印中能够看到性能彻底不会受影响;
为了进一步优化并减小反射调用,仅在用户触发侧滑、侧滑彻底闭合时修改Activity透明属性
public void setWindowToTranslucent(boolean translucent) {
if (isTranslucentWindow == translucent || !isSlidingEnabled()){
return;
}
isTranslucentWindow = translucent;
if (isTranslucentWindow) {
convertActivityToTranslucent(((Activity) getContext()));
} else {
convertActivityFromTranslucent(((Activity) getContext()));
}
}
复制代码
因为是系统Api 在不一样版本会略有不一样,作了版本区分。并对反射Api作了try/catch保护,在反射Api调用异常的状况下,不会对App功能有影响。原Activity windowIsTranslucent 属性不变
设置windowIsTranslucent =true 后,退后台再打开App时上层的Activity 会被再次绘制
Activity 替换主题的两种方式
1.在9.0后 @hide Api 经过反射是没法调用,后续是解决方案 2.除了修改windowIsTranslucent 尚未有其余的解决方案? 3.如何从根源思考、解决问题