窗口最小化后任务栏动画化_动画化Android活动过渡

窗口最小化后任务栏动画化

学习Android开发 (Learning Android Development)

Android provides a lot of Animation APIs. However, only one can be used to animate the Window Transition such as Activity to Activity transition. This is view.animation feature, and that’s available since API 1 (i.e. it is the original and oldest animation feature available).

被A ndroid提供了很多动画的API。 但是,只能使用一个来设置窗口过渡的动画,例如“活动到活动”过渡。 这是view.animation功能,自API 1起可用(即, 它是原始的和最早的动画功能 )。

The view.animation is considered deprecated, but is still made available just for the purpose window animation. Nonetheless, it is good to learn how to use it since it is the only way, and there’s no formal tutorial out there I found about it.

view.animation被认为已弃用,但仍仅可用于目标窗口动画。 尽管如此,最好还是学习使用方法,因为这是唯一的方法,而且我没有找到关于它的正式教程。

将活动1动画化为活动2,然后返回 (Animate Activity 1 to Activity 2 and back)

In this article, I will show you how to achieve the below animation.

在本文中,我将向您展示如何实现以下动画。

  • Activity 1 = Hello World; Activity 2 = Purple color

    活动1 = Hello World; 活动2 =紫色
Image for post

Assuming you want all your activities transitions to animate as above. You just need to define 2 animations.

假设您希望所有活动过渡都如上所述进行动画处理。 您只需要定义2个动画。

在XML文件中定义动画。 (Defining the animation in the XML file.)

In your project, create a folder named anim in res folder, i.e. res/anim. Then create the below 2 XML which defines the animations

在您的项目中,在res文件夹中创建一个名为anim的文件夹,即res/anim 。 然后创建下面的2个XML,定义动画

enter_activity.xml (animate from size 0% to 100%)

enter_activity.xml (从大小0%到100%进行动画处理)

<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="
@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="2000" />
</set>

exit_activity.xml. (animate from size 100%to 0%)

exit_activity.xml 。 (从大小100%调整为0%)

<set android:shareInterpolator="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="
@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="false"
android:duration="2000" />
</set>

在styles.xml中设置它们 (Set them in the styles.xml)

After you have created the 2 XMLs, you’ll need to refer to them in your styles.xml.

创建2个XML之后,需要在styles.xml中引用它们。

There will need to be 2 sets of them though,

不过,将需要两套,

  1. For entering the Activity 2 (i.e Activity 1 exiting and Activity 2 entering)

    用于输入活动2(即,活动1退出而活动2进入)
  2. For exiting Activity 2 (i.e. Activity 2 exiting and Activity 1 entering).

    用于退出活动2(即,活动2退出而活动1进入)。
Image for post

By understanding this, now we can define it as a style of animation we want to use for all our activity transitions. We can do it in the project style.xml as shown below.

通过了解这一点,现在我们可以将其定义为我们要用于所有活动过渡的动画样式。 我们可以在如下所示的项目style.xml中进行操作。

<resources>
<style name="AppTheme" ... >
<!-- Other attributes here -->
<item name="android:windowAnimationStyle">
@style/WindowAnimations</item>
</style>
<style name="WindowAnimations">
<item name="android:activityOpenEnterAnimation">
@anim/enter_activity</item>
<item name="android:activityOpenExitAnimation">
@anim/exit_activity</item>
<item name="android:activityCloseEnterAnimation">
@anim/enter_activity</item>
<item name="android:activityCloseExitAnimation">
@anim/exit_activity</item>
</style>
</resources>

Note, the above assume that your AndroidManifest.xml application is pointing to the AppTheme (i.e. having android:theme=”@style/AppTheme") in style.xml

请注意,以上假设您的AndroidManifest.xml应用程序指向AppTheme中的AppTheme (即android:theme=”@style/AppTheme" )

With this done, you are set to run your project, and it will animate as you transition from one activity to the other.

完成此操作后,您就可以运行您的项目,并且在您从一个活动过渡到另一个活动时,它将进行动画处理。

You can have the code here.

您可以在此处获取代码。

为不同的活动处理不同的过渡 (Handling different transition for different activities)

The above enforces the animations across all activities transition. If you like to handle it individually within for each activity, then you have to discard the styles.xml code above.

上面的代码在所有活动过渡中强制执行动画。 如果您想在每个活动中单独处理它,则必须放弃上面的styles.xml代码。

Instead, for each activity, use the overridePendingTransition function instead. Do it in the onCreate and finish life cycle callback functions.

相反,对于每个活动,请改用overridePendingTransition函数。 在onCreate finishfinish生命周期回调函数。

class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
overridePendingTransition(
R.anim.enter_activity, R.anim.exit_activity)
}
override fun finish() {
super.finish()
overridePendingTransition(
R.anim.enter_activity, R.anim.exit_activity)
}
}

动画打开/关闭应用程序 (Animating opening/closing the App)

Ideally, the above animation should also work for

理想情况下,以上动画也应适用于

  • Opening (Resuming) the App

    打开(恢复)应用
  • Existing (Pausing) the App

    现有(暂停)该应用

The way to define them is like what described above, using styles.xml, with a different attribute key i.e. windowEnterAnimation and windowExitAnimation

定义它们的方法类似于上面所述,使用styles.xml ,具有不同的属性键,即windowEnterAnimationwindowExitAnimation

<style name="WindowAnimations">
<item name="android:windowEnterAnimation">
@anim/enter_activity</item>
<item name="android:windowExitAnimation">
@anim/exit_activity</item>
</style>

However, when I tested out, there seems to have some issues as below.

但是,当我进行测试时,似乎存在以下一些问题。

  1. The windowExitAnimation just doesn’t work

    windowExitAnimation无效

  2. The windowEnterAnimation although work, it interferes with activityOpenEnterAnimation and activityCloseEnterAnimation animation.

    windowEnterAnimation虽然有效,但会干扰activityOpenEnterAnimationactivityCloseEnterAnimation动画。

I have reported the issue to Google in https://issuetracker.google.com/issues/157674884. Maybe Nick Butcher could help check it out?

我已通过https://issuetracker.google.com/issues/157674884向Google报告了此问题。 也许尼克·布彻(Nick Butcher)可以帮忙检查一下?

Thanks for reading. You can check out my other topics here.

谢谢阅读。 您可以在此处查看我的其他主题。

To follow me, you can subscribe here, follow on Medium, Twitter, Facebook, or Reddit for little tips and learning on mobile development, medium writing, etc related topics. ~Elye~

要关注我,您可以在此处订阅,在Medium Twitter Facebook Reddit 上关注 获取有关移动开发,媒体写作等相关主题的小技巧和学习。 〜艾莉〜

翻译自: https://levelup.gitconnected.com/animate-android-activities-transition-bf7f89a74b35

窗口最小化后任务栏动画化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值