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修改方式
- AndroidManifest 设置Activity Theme
- 在Activity setContentView执行前 调用setTheme
可以通过其他方式修改Activity windowIsTranslucent 属性吗?
方案B+:反射动态设置Activity windowIsTranslucent
查阅Activity源码,看一下他是如何酿成透明的
/**
- Convert a translucent themed Activity {@link android.R.attr#windowIsTranslucent} back from
- opaque to translucent following a call to {@link #convertFromTranslucent()}.
-
- 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.
-
- 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()));
}
自我介绍一下,小编13年上海交大结业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,每每是自己摸索发展大概是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学结果低效又漫长,而且极易遇到天花板技能停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简朴,就是希望可以或许资助到想自学提升又不知道该从何学起的朋侪,同时减轻大家的负担。
既有得当小白学习的零基础资料,也有得当3年以上经验的小伙伴深入学习提升的进阶课程,根本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比力大,这里只是将部门目录截图出来,每个节点里面都包罗大厂面经、学习条记、源码讲义、实战项目、讲解视频,而且会持续更新!
假如你以为这些内容对你有资助,可以扫码获取!!(备注:Android)
结语
由于篇幅限制,文档的详解资料太全面,细节内容太多,以是只把部门知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是目录截图:
由于整个文档比力全面,内容比力多,篇幅不允许,下面以截图方式展示 。
再附一部门Android架构面试视频讲解:
《互联网大厂面试真题分析、进阶开发焦点学习条记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
ps://img2.imgtp.com/2024/03/13/H4lCoPEF.jpg" />
结语
由于篇幅限制,文档的详解资料太全面,细节内容太多,以是只把部门知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是目录截图:
[外链图片转存中…(img-sSuGGVzB-1712652371524)]
由于整个文档比力全面,内容比力多,篇幅不允许,下面以截图方式展示 。
再附一部门Android架构面试视频讲解:
[外链图片转存中…(img-dmhbSNo0-1712652371525)]
《互联网大厂面试真题分析、进阶开发焦点学习条记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |