如何在 Android 中的两个活动之间应用幻灯片动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10243557/
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 apply slide animation between two activities in Android?
提问by Jignesh Ansodariya
I want to achieve a sliding effect from left to right when I move from one activity to another. For that I am using the following code, but I am not getting any results. Please correct me.
当我从一个活动移动到另一个活动时,我想实现从左到右的滑动效果。为此,我使用了以下代码,但没有得到任何结果。请纠正我。
In java both files:
在java中两个文件:
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.main);
Two files in res/anim directory:
res/anim 目录下的两个文件:
fadein.xml
淡入淡出.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/slide_out_right"
android:toAlpha="1.0" >
</alpha>
fadeout.xml
淡出.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/slide_in_left"
android:toAlpha="1.0" >
</alpha>
采纳答案by MAC
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
public void run() {
/* Create an intent that will start the main activity. */
Intent mainIntent = new Intent(SplashScreen.this,
ConnectedActivity.class);
mainIntent.putExtra("id", "1");
//SplashScreen.this.startActivity(mainIntent);
startActivity(mainIntent);
/* Finish splash activity so user cant go back to it. */
SplashScreen.this.finish();
/* Apply our splash exit (fade out) and main
entry (fade in) animation transitions. */
overridePendingTransition(R.anim.mainfadein,R.anim.splashfadeout);
}
}, SPLASH_DISPLAY_TIME);
}
回答by Born To Win
Add this two file in res/anim folder.
在 res/anim 文件夹中添加这两个文件。
R.anim.slide_out_bottom
R.anim.slide_out_bottom
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="@integer/time_duration_max"
android:fromXDelta="0%"
android:fromYDelta="100%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
R.anim.slide_in_bottom
R.anim.slide_in_bottom
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="@integer/time_duration_max"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="100%" />
</set>
And write the below line of code in your view click listener.
并在您的视图单击侦听器中编写以下代码行。
startActivity(new Intent(MainActivity.this, NameOfTargetActivity.class));
overridePendingTransition(R.anim.slide_out_bottom, R.anim.slide_in_bottom);
回答by Shohan Ahmed Sijan
You can overwrite your default activity animation and it perform better than overridePendingTransition. I use this solution that work for every android version. Just copy paste 4 files and add a 4 lines style as below:
您可以覆盖默认的活动动画,它的性能比 overridePendingTransition 更好。我使用这个适用于每个 android 版本的解决方案。只需复制粘贴 4 个文件并添加如下 4 行样式:
Create a "CustomActivityAnimation" and add this to your base Theme by "windowAnimationStyle".
创建一个“CustomActivityAnimation”并通过“windowAnimationStyle”将其添加到您的基本主题中。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>
</style>
<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
<item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
<item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
</style>
Then Create anim folder under res folder and then create this four animation files into anim folder:
然后在 res 文件夹下创建 anim 文件夹,然后将这四个动画文件创建到 anim 文件夹中:
slide_in_right.xml
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
slide_out_left.xml
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
slide_in_left.xml
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
slide_out_right.xml
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
If you face any problem then you can download my sample project from github.
如果您遇到任何问题,那么您可以从 github下载我的示例项目。
Thanks
谢谢
回答by Phan Van Linh
Slide up/down with alpha animation with a few note
使用 alpha 动画向上/向下滑动,并附上几个音符
slide_up.xml
幻灯片_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/activity_transition_time"
>
<translate
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:fromAlpha="0.5"
android:toAlpha="1"/>
</set>
slide_down.xml
幻灯片文件.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/activity_transition_time"
>
<translate
android:fromYDelta="0"
android:toYDelta="100%p"/>
<alpha
android:fromAlpha="1"
android:toAlpha="0.5"/>
</set>
no_animation.xml
no_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@integer/activity_transition_time"
android:fromYDelta="0"
android:toYDelta="0"/>
First Activity
第一次活动
startActivity(new Intent(this, SecondActivity.class));
overridePendingTransition(R.anim.slide_up, R.anim.no_animation); // remember to put it after startActivity, if you put it to above, animation will not working
// document say if we don't want animation we can put 0. However, if we put 0 instead of R.anim.no_animation, the exist activity will become black when animate
Second Activity
第二个活动
finish();
overridePendingTransition(R.anim.no_animation, R.anim.slide_down);
Done
完毕
MORE
I try to make the slide animation like iOS animation when present a View Model (like this https://www.youtube.com/watch?v=deZobvh2064) but failed.
更多
我尝试在呈现视图模型(例如https://www.youtube.com/watch?v=deZobvh2064)时制作类似于 iOS 动画的幻灯片动画,但失败了。
Looking at iOS present animation you will see: The animation from bottom with alpha (about 50%) then it go very fast then slower, the animation time is about > 500ms (I use trim video tools for count the animation time https://www.kapwing.com/trim-videoso it can not exactly 100%)
查看iOS当前动画,您将看到:从底部开始的带有alpha的动画(大约50%)然后它变得非常快然后变慢,动画时间大约> 500ms(我使用修剪视频工具来计算动画时间 https:// www.kapwing.com/trim-video所以它不能完全 100%)
Then I try to apply to android.
To make alpha I use <alpha>
and success.
To make animation start faster than slower I use android:interpolator="a decelerate interpolator"
but it almost failed.
然后我尝试申请android。
为了制作我使用的 alpha<alpha>
并取得成功。
为了使动画开始的速度比我使用的慢,android:interpolator="a decelerate interpolator"
但它几乎失败了。
There are 3 default decelerate interpolator
in Android@android:interpolator/decelerate_quad
-> factor = 1@android:interpolator/decelerate_cubic
-> factor = 1.5@android:interpolator/decelerate_quint
_> factor = 2.5
(higher factor <=> animation start more faster from start and more slower at end)
Here is a good tutorial http://cogitolearning.co.uk/2013/10/android-animations-tutorial-5-more-on-interpolators/for understand it
decelerate interpolator
Android 中有 3 个默认值@android:interpolator/decelerate_quad
-> 因子 = 1 @android:interpolator/decelerate_cubic
-> 因子 = 1.5 @android:interpolator/decelerate_quint
_> 因子 = 2.5
(更高的因子 <=> 动画从开始开始更快,结束时更慢)
这是一个很好的教程http://cogitolearning。 co.uk/2013/10/android-animations-tutorial-5-more-on-interpolators/了解它
I tried 3 above I can not achieve like iOS, the animation can not start faster like iOS. Then I create a custom decelerateInterpolator wiht factor = 3 like
我尝试了上面的3个我无法像iOS那样实现,动画无法像iOS那样启动得更快。然后我创建一个自定义的 decelerateInterpolator,因子为 3,例如
<?xml version="1.0" encoding="utf-8"?>
<decelerateInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="3" />
and I increase the duration time from 500 -> 750
. It working well (very similar to iOS). However, it only working well in some device, in some device the animation is quite slow. Later on, I know that animation may different on different device (eg: some device will faster and some device will slower) so I can not make it the animation similar in all Android device. Therefore I don't use interpolator
. I don't know if my testing is exactly 100% or not but I hope this experience help
我从500 -> 750
. 它运行良好(与 iOS 非常相似)。然而,它只在某些设备上运行良好,在某些设备中动画很慢。后来,我知道动画在不同的设备上可能会有所不同(例如:有些设备会更快,有些设备会更慢)所以我不能让它在所有 Android 设备上都具有相似的动画。因此我不使用interpolator
. 我不知道我的测试是否完全是 100% 但我希望这个经验有帮助
回答by Rohit Singh
Here is a Slide Animation for you.
这是为您准备的幻灯片动画。
Let's say you have two activities.
假设您有两个活动。
- MovieDetailActivity
- AllCastActivity
- 电影细节活动
- AllCastActivity
And on click of a Button, this happens.
单击按钮时,就会发生这种情况。
You can achieve this in 3 simple steps
您可以通过 3 个简单的步骤实现这一目标
1) Enable Content Transition
1) 启用内容转换
Go to your style.xml
and add this line to enable the content transition.
转到您的style.xml
并添加此行以启用内容转换。
<item name="android:windowContentTransitions">true</item>
2) Write Default Enter and Exit Transition for your AllCastActivity
2) 为您的 AllCastActivity 编写默认的进入和退出转换
public void setAnimation()
{
if(Build.VERSION.SDK_INT>20) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.LEFT);
slide.setDuration(400);
slide.setInterpolator(new AccelerateDecelerateInterpolator());
getWindow().setExitTransition(slide);
getWindow().setEnterTransition(slide);
}
}
3) Start Activity with Intent
3) 有意图地开始活动
Write this method in Your MovieDetailActivity
to start AllCastActivity
在你MovieDetailActivity
的开始写这个方法AllCastActivity
public void startActivity(){
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putStringArrayListExtra(MOVIE_LIST, movie.getImages());
if(Build.VERSION.SDK_INT>20)
{
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(BlankActivity.this);
startActivity(i,options.toBundle());
}
else {
startActivity(i);
}
}
Most important!
最重要的!
put your setAnimation()
method before setContentView()
method otherwise the animation will not work.
So your AllCastActivity.java
should look like this
将您的setAnimation()
方法放在方法之前,setContentView()
否则动画将无法工作。
所以你AllCastActivity.java
应该看起来像这样
class AllCastActivity extends AppcompatActivity {
@Override
protected void onCreate(Bundle savedInstaceState)
{
super.onCreate(savedInstaceState);
setAnimation();
setContentView(R.layout.all_cast_activity);
.......
}
private void setAnimation(){
if(Build.VERSION.SDK_INT>20) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.LEFT);
..........
}
}
回答by Hitesh Sahu
Kotlin example:
科特林示例:
private val SPLASH_DELAY: Long = 1000
internal val mRunnable: Runnable = Runnable {
if (!isFinishing) {
val intent = Intent(applicationContext, HomeActivity::class.java)
startActivity(intent)
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
finish()
}
}
private fun navigateToHomeScreen() {
//Initialize the Handler
mDelayHandler = Handler()
//Navigate with delay
mDelayHandler!!.postDelayed(mRunnable, SPLASH_DELAY)
}
public override fun onDestroy() {
if (mDelayHandler != null) {
mDelayHandler!!.removeCallbacks(mRunnable)
}
super.onDestroy()
}
put animations in anim folder:
将动画放在 anim 文件夹中:
slide_in.xml
幻灯片输入文件
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0%p">
</translate>
slide_out.xml
幻灯片输出文件
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXDelta="0%p"
android:toXDelta="-100%p">
</translate>
USAGE
用法
navigateToHomeScreen();
回答by P Kuijpers
You could use overridePendingTransition
in startActivity
instead of onCreate
. At least, that works for me!
您可以使用overridePendingTransition
instartActivity
而不是onCreate
. 至少,这对我有用!
See a full example here. It's including an (reverse) animation onBackPressed, so while returning to the previous activity! In your specific example (fade-in and -out) that might be unnecessary though.
在此处查看完整示例。它包括一个(反向)动画 onBackPressed,所以在返回上一个活动时!在您的具体示例(淡入和淡出)中,这可能是不必要的。
回答by Arnav Rao
Slide animation can be applied to activity transitions by calling overridePendingTransition and passing animation resources for enter and exit activities.
通过调用 overridePendingTransition 并为进入和退出活动传递动画资源,可以将幻灯片动画应用于活动转换。
Slide animations can be slid right, slide left, slide up and slide down.
幻灯片动画可以向右滑动、向左滑动、向上滑动和向下滑动。
Slide up
向上滑动
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
Slide down
滑下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
overridePendingTransition(R.anim.slide_down, R.anim.slide_up);
See activity transition animation examplesfor more activity transition examples.