Android 如何实现从右到左的动画开始活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26431017/
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
How to achieve right to left animation to start the activity
提问by N Sharma
I am working on one demo application where I want to apply animation whenever app start any activity
. I wrote below code but this is for to animate the activity from left to right.
我正在开发一个演示应用程序,我想在应用程序启动任何activity
. 我写了下面的代码,但这是为了从左到右为活动设置动画。
left_to_right.xml
left_to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
right_to_left.xml
right_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
I am here starting an activity
like this
我在这里开始activity
这样的
startActivity(new Intent(this, LoginActivity.class));
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
I want to achieve an animation right to left. How this can be done.
我想实现从右到左的动画。如何做到这一点。
Thanks in advance.
提前致谢。
回答by Mike
Do these modifications to your animation files:
对动画文件进行以下修改:
enter.xml:
输入.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
exit.xml:
退出.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" />
</set>
You'll have your second activity sliding in from right to the left.
您的第二个活动将从右向左滑动。
For a better understanding on how to play around with the fromXDelta and toXDelta values for the animations, here is a very basic illustration on the values:
为了更好地理解如何处理动画的 fromXDelta 和 toXDelta 值,这里有一个非常基本的值说明:
This way you can easily understand why you add android:fromXDelta="0%" and android:toXDelta="-100%" for your current activity. And this is because you want it to go from 0% to the -100% position.
通过这种方式,您可以轻松理解为什么要为当前活动添加 android:fromXDelta="0%" 和 android:toXDelta="-100%"。这是因为您希望它从 0% 到 -100% 的位置。
[EDIT]
[编辑]
So if you want to open ActivityB from ActivityA you do the following(let's say you have a button):
因此,如果您想从 ActivityA 打开 ActivityB,请执行以下操作(假设您有一个按钮):
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ActivityA.this, ActivityB.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
}
});
Now, if you want to have the "backwards" animation of the first one, when you leave Activity B, you'll need 2 new animation files and some code in the ActivityB's onBackPressed method, like this:
现在,如果您想拥有第一个的“向后”动画,当您离开 Activity B 时,您将需要 2 个新动画文件和 ActivityB 的 onBackPressed 方法中的一些代码,如下所示:
First the animation files: left_to_right.xml:
首先是动画文件:left_to_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
right_to_left.xml:
right_to_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
And in ActivityB do the following:
在 ActivityB 中执行以下操作:
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
Also if you have up navigation enabled, you'll have to add the animation in this case as well:
此外,如果您启用了向上导航,则在这种情况下还必须添加动画:
You enable UP navigation like this:
您可以像这样启用 UP 导航:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
And this is how you handle the animation in this case too:
在这种情况下,这也是您处理动画的方式:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
finish();
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
return true;
}
return super.onOptionsItemSelected(item);
}
Also be aware that even if your code is okay, your phone might have animation turned off. To turn then on do the following:
另请注意,即使您的代码没问题,您的手机也可能关闭了动画。要打开然后执行以下操作:
- Open Settings and go to Developer Options
- Make sure it is enabled (By sliding the toggle button on the top right)
- Scroll down and under Drawing, tap these options one by one: Windows animation scale, Transition animation scale, and Animator duration scale
- Select "Animation scale 1x"
- 打开设置并转到开发人员选项
- 确保它已启用(通过滑动右上角的切换按钮)
- 向下滚动并在“绘图”下,一一点击这些选项:Windows 动画比例、过渡动画比例和 Animator 持续时间比例
- 选择“动画比例 1x”
Does this help?
这有帮助吗?
回答by soundsofpolaris
overridePendingTransition
should be called in the "target" activity. For example: Going from Activity A -> B, you would put the overridePendingTransition
call in the onCreate
of Activity B.
overridePendingTransition
应该在“目标”活动中调用。例如:从 Activity A -> B 开始,您将overridePendingTransition
调用放在onCreate
Activity B 的 中。
Keep in mind, if the user has disabled animation on a system level, you can't force animations to show.
请记住,如果用户在系统级别禁用了动画,则无法强制显示动画。
EDIT:
编辑:
An example would look like this:
一个示例如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.enter, R.anim.exit);
}
回答by Maulik Patel
This is the perfect code for me Slideinleft
这对我来说是完美的代码 Slideinleft
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="800"/>
Slideinright
向右滑动
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="800"/>
In Activity
活动中
Intent intent = new Intent(getApplicationContext(),termcondionactivity.class);
Bundle bndlAnimation = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.slideinleft, R.anim.slideinright).toBundle();
startActivity(intent, bndlAnimation);
回答by saranya
Try this code, it's working for me
试试这个代码,它对我有用
To slide from right to left
从右向左滑动
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0%"
android:toXDelta="-50%" >
</translate>
</set>
To slide from left to right
从左向右滑动
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="-50%"
android:toXDelta="0%" >
</translate>