-
{@link TranslucentConversionListener#onTranslucentConversionComplete} is called the image
-
behind the frontmost Activity will be indeterminate.
-
-
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
true
if Window was opaque and will become translucent or -
false
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.
-
-
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.
-
-
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透明属性;
-
convertToTranslucent //将当前Activity Window 设置为透明
-
convertFromTranslucent //将当前 Activity Window 设置为非透明
由于是系统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 替换主题的两种方式
-
AndroidManifest 设置Activity Theme
-
在Activity setContentView执行前 调用setTheme
- Activity 源码分析
-
convertToTranslucent //将当前Activity Window 设置为透明
-
convertFromTranslucent //将当前 Activity Window 设置为非透明
- 反射调用
思考
–
1.在9.0后 @hide Api 通过反射是无法调用,后续是解决方案 2.除了修改windowIsTranslucent 还没有有其他的解决方案? 3.如何从根源思考、解决问题
最后我想说:对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/57e6a528a012b6ce45b8ba9c97b4ece7.jpeg)
最后
**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)**
![](https://i-blog.csdnimg.cn/blog_migrate/57e6a528a012b6ce45b8ba9c97b4ece7.jpeg)
最后
**要想成为高级安卓工程师,必须掌握许多基础的知识。**在工作中,这些原理可以极大的帮助我们理解技术,在面试中,更是可以帮助我们应对大厂面试官的刁难。
[外链图片转存中…(img-3sHpHhC0-1712434841387)]
[外链图片转存中…(img-NRowptKQ-1712434841388)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!