Android overridePendingTransition 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20745079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
overridePendingTransition not working
提问by Tim Kranen
I'm trying to implement a transition in my app but overridePendingTransition(anim, anim) is not working correctly.
我正在尝试在我的应用程序中实现过渡,但 overridePendingTransition(anim, anim) 无法正常工作。
- I have window transitions enabled
- After debugging the code I can say that the compiler does execute the call but it is NOT shown
- I have tried calling finish() before overridePendingTransition() this does not seem to have any effect
- 我启用了窗口转换
- 调试代码后,我可以说编译器确实执行了调用,但未显示
- 我试过在 overridePendingTransition() 之前调用 finish() 这似乎没有任何效果
My code is simple and standard:
我的代码简单而标准:
Starting the intent and calling overridePendingTransition:
启动意图并调用 overridePendingTransition:
Intent newsIntent = new Intent(ZPFActivity.this, More2013Activity.class);
startActivity(newsIntent);
overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
finish();
The start animation should not do anything only the fade animation should have effect.
开始动画不应该做任何事情,只有淡入淡出动画才应该有效果。
slide_no_move XML:
slide_no_move XML:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p" android:toYDelta="0%p"
android:duration="500"/>
fade XML:
淡出 XML:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"/>
</set>
EDIT:I forgot to mention the fact that the activities that I start all extendthe 'main' activity. Could this be the fact that is causing the problem?
编辑:我忘了提到我开始的活动都扩展了“主要”活动的事实。这可能是导致问题的事实吗?
采纳答案by Tim Kranen
I know this is an old post, but I saw it and felt obligated to answer it:
我知道这是一个旧帖子,但我看到它并觉得有义务回答它:
It was a very obscure problem, and it was device related. I was using an iOcean X7 at the time. Later I found out that this Phone was not able to play animations. For some it did not play animations on ANY app.
这是一个非常模糊的问题,并且与设备有关。我当时使用的是 iOcean X7。后来我发现这个手机不能播放动画。对于某些应用程序,它没有在任何应用程序上播放动画。
回答by Damien R.
Try to call the pendingTransition after calling finish()
, like this:
尝试在调用后调用 pendingTransition finish()
,如下所示:
Intent newsIntent = new Intent(ZPFActivity.this, More2013Activity.class);
startActivity(newsIntent);
finish();
overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
回答by Jefferson Fidencio
If you activate usb debugging mode, it can disable the transitions effects. Enter Developer options (where you activated debugging mode), find the user's interface section, check if the animation's transition scale have the animations disabled. If so, set it to .5x. Done!
如果您激活 USB 调试模式,它可以禁用过渡效果。进入开发者选项(你激活调试模式的地方),找到用户界面部分,检查动画的过渡比例是否禁用了动画。如果是这样,请将其设置为 0.5 倍。完毕!
回答by h4rd4r7c0r3
Try using this :
尝试使用这个:
public class More2013Activity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim. slide_no_move, R.anim.fade_in);
setContentView(R.layout.more_activity)
}
}
instead of your code.
而不是你的代码。
回答by Prasad
If you are using android version >= 21 then make the android:windowActivityTransitionsattribute to truein the style.xmlfile in the values-v21folder as follows.
如果您使用的是 android 版本 >= 21,则在values-v21文件夹中的style.xml文件中将android:windowActivityTransitions属性设置为true,如下所示。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowActivityTransitions">true</item>
</style>
回答by koteswara D K
from 6.0 use this method to override the custom animation
从 6.0 开始使用此方法覆盖自定义动画
ActivityOptions options = ActivityOptions.makeCustomAnimation(context,R.anim.slide_from_right, R.anim.slide_to_left);
Intent intent = new Intent(context, WhichActivityToOpen.class);
context.startActivity(intent, options.toBundle());
回答by user1154390
Go in your device Developer Options, There are three further options windows animation scale, Transition Animation scale and Animation duration scale. Make sure all are not off. This is one of the reason I came across.
进入您的设备开发人员选项,还有另外三个选项窗口动画比例、过渡动画比例和动画持续时间比例。确保没有全部关闭。这是我遇到的原因之一。
回答by Mehran Elyasi
Probably you are using overridePendingTransition(anim, anim)in your onPause()method too. I had the same problem but I solve it by a tiny trick.
也许你正在使用overridePendingTransition(动画,动画)在你的onPause()方法了。我有同样的问题,但我通过一个小技巧解决了它。
1:Define a boolean in your class before onCreate()method
1:在onCreate()方法之前在您的类中定义一个布尔值
private boolean trans=true;
@Override
protected void onCreate(Bundle savedInstanceState) {...
2:Put trans = false;when you you use startActivity
2:把反式=假;当你使用 startActivity
Intent newsIntent = newIntent(ZPFActivity.this,More2013Activity.class);
trans = false;
startActivity(newsIntent);
overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
3:And in your onPause()method do this
3:并在您的onPause()方法中执行此操作
@Override
protected void onPause() {
if (trans) {
overridePendingTransition(R.anim.slide_no_move, R.anim.fade);
}
super.onPause();
}
4:And in your onResume()method do this
4:并在您的onResume()方法中执行此操作
@Override
protected void onResume() {
super.onResume();
trans=true;
}
回答by Anshad Ali KM
please check your device is in power saving mode. in power saving mode animations will not work.
请检查您的设备是否处于省电模式。在省电模式下动画将不起作用。
回答by rmirabelle
The challenge for me was not with execution, but rather proper conceptualization.
对我来说,挑战不在于执行,而在于正确的概念化。
The overridePendingTransition
method accepts an enteranimation and an exitanimation.
该overridePendingTransition
方法接受进入动画和退出动画。
The trick to conceptualizing these properly is that enterand exitwill have different meanings depending on WHERE we call overridePendingTransition
:
正确概念化这些的技巧是进入和退出将具有不同的含义,具体取决于我们调用的位置overridePendingTransition
:
- When called from
onResume
, the currentactivity is entering and the previousactivity (whatever it was) is exiting. - When called immediately after
startActivity
, the nextactivity (being started) is entering and the currentactivity is exiting. - When called from
finish
, the nextactivity (whatever it winds up being) is entering and the currentactivity is exiting.
- 当从 调用时
onResume
,当前活动正在进入,而前一个活动(无论它是什么)正在退出。 - 在 之后立即调用时
startActivity
,下一个活动(正在启动)正在进入,当前活动正在退出。 - 当从 调用时
finish
,下一个活动(无论它最终是什么)正在进入,而当前活动正在退出。
Once I understood these rather weird enter/exit rules, I was able to accomplish proper transitions from anywhere to anywhere.
一旦我理解了这些相当奇怪的进入/退出规则,我就能够完成从任何地方到任何地方的正确转换。
Hope this helps.
希望这可以帮助。