主题切换时的过渡(或 Activity.recreate()) [英] Transition when theme switching (or Activity.recreate())

查看:111
本文介绍了主题切换时的过渡(或 Activity.recreate())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是 API 级别 14+,所以我使用了 Activity.recreate() 用于在我的应用中切换主题.效果很好,除了黑屏会闪烁大约 0.5 秒.

I'm targeting API level 14+, so I've used Activity.recreate() for theme switching in my app. It worked out nice, except a black screen will flash for about 0.5s.

我曾经认为这里不可能提供更好的体验,直到我看到一个材质设计的应用程序,它成功地通过交叉淡入淡出切换了它的主题.但很遗憾,我前段时间从手机上卸载了它,所以一直想不起来它的名字.

I used to think it is impossible to provide a better experience here, until I saw a material designed app which successfully switched its theme with a crossfade. But unluckily I failed to recall its name since I uninstalled it from my phone some time ago.

我尝试过 Activity.overridePendingTransition(),它没有用,我相信原因是 recreate() 调用很像配置更改,因此机制不同于完成和启动全新的活动.

I've tried Activity.overridePendingTransition(), it did not work, and I believe the reason is that the recreate() call is much like a configuration change so the mechanism is different from finishing and launching a brand new activity.

(在试图找到那个应用程序时,我遇到了另一个神奇的主题切换应用程序,即使没有调用 recreate().嗯……有人知道这个技巧是怎么做的吗?)

(And while trying to find that app I came across another magic theme switching app, even without calling recreate(). Hum... Anybody know how the trick is done?)

动态主题切换 GIF

谁说主题是一成不变的?更改...上的主色和强调色

我发现上面的 GIF 有点分散了原始问题的注意力,所以我把它变成了一个链接.

I found that the GIF above is kind of a distraction from the original question, so I turned it into a link.

我最初的问题是,是否有任何方法可以通过适当的过渡来更改(切换)主题?

My original question is that, is there any method to change (switch) theme with an appropriate transition?

通过蛮力更改屏幕上的所有内容并不是切换应用程序的主题;虽然看起来是这样,但它可能会导致很多问题.

Changing all the things on screen by Brute force is not switching the app's theme; it just appear to be though, but it can lead to many problems.

悲哀地发现了上述GIF可能只是哈克蛮力攻击,但仍感谢@Emanuel Moecklin指出了这一点.

Sad to find out the above GIF may only be a hacky brute force attack, but still thanks to @Emanuel Moecklin for pointing this out.

但我仍然希望能出现一个(真正)切换主题与过渡"的解决方案,或者有人告诉我 Activity.recreate() 不能动画并且没有其他方法可以更好地切换主题.

But I still hope a "(truly) switching theme with transition" solution can come up, or someone tell me that Activity.recreate() cannot be animated and no other ways can switch themes better.

推荐答案

经过这么多年,我学会了一个简单的方法.

After all these years I've learned an easy way.

代替 Activity.recreate(),使用以下代码片段:

Instead of Activity.recreate(), use the follwing code fragment:

finish();
startActivity(new Intent(this, getClass()));
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

它会给你一个很好的主题之间的动画.

And it will give you a nice animation between themes.

奖励:您可以手动保存 &恢复实例状态以保留 UI 状态,并通过覆盖 Activity 上的输入相关方法(dispatch*Event())在重新启动时忽略用户输入.

Bonus: You can manually save & restore instance state to preserve UI state, and ignore user input during restarting by overriding input related methods (dispatch*Event()) on Activity.

这篇关于主题切换时的过渡(或 Activity.recreate())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆