Android activity动画无效问题汇总 您所在的位置:网站首页 uniapp转场动画时间 Android activity动画无效问题汇总

Android activity动画无效问题汇总

2023-09-26 00:44| 来源: 网络整理| 查看: 265

概述

android activity动画是一个比较简单的功能。 但是使用时总会由于各种小问题导致动画失效,笔者根据自己经验,整理了各种可能导致的原因,期望能对你有所帮助。

activity动画方式 在AndroidMenifest中添加activity的动画属性windowAnimationStyle @style/anim_fade 在activity代码中添加 overridePendingTransition overridePendingTransition(int enterAnim,int exitAnim) 问题汇总 一、动画写的有问题二、activity theme中设置动画为null,或者parent theme设置动画为null三、overridePendingTransition 使用时机问题四、overridePendingTransition 写错地方五、onPause与onResume中的overridePendingTransition会覆盖其他位置六、透明度影响动画七、插件化问题导致找不到动画 一、动画写的有问题

动画本身出问题的方式无法一一列举,常见的有“duration设置为0”,“from与to的值设置相同”。

二、activity theme中设置动画为null,或者parent theme设置动画为null

如下:

true true @null true true @null 三、overridePendingTransition 使用时机问题

overridePendingTransition 源码注释如下:

Call immediately after one of the flavors of startActivity(Intent) or finish to specify an explicit transition animation to perform next. As of Build.VERSION_CODES.JELLY_BEAN an alternative to using this with starting activities is to supply the desired animation information through a ActivityOptions bundle to startActivity(Intent, Bundle) or a related function. This allows you to specify a custom animation even when starting an activity from outside the context of the current top activity. Params: enterAnim – A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation. exitAnim – A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.

其中说了两个overridePendingTransition 的使用时机:

在startActivity 之后在finish之后

如下:

startActivity(intent); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); finish(); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 四、overridePendingTransition 写错地方

写错地方就纯属是开发者的粗心,例子如下:

重写了finish方法,但是调用的是finishAndRemoveTask

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); finishAndRemoveTask(); } @Override public void finish() { super.finish(); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } 五、onPause与onResume中的overridePendingTransition会覆盖其他位置

根据笔者经验,onPause和onResume中如果写了overridePendingTransition,那么其效果会覆盖其他地方设置的动画。 比如你在finish的时候设置了overridePendingTransition,然后在onPause中也设置了overridePendingTransition,那么最终效果会以onPause中的。 比如下面的例子中,finish之后设置了动画,onPause中关闭了activity的动画,那么最终就是没有动画。

@Override protected void onPause() { super.onPause(); overridePendingTransition(0,0) } @Override public void finish() { super.finish(); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } 六、透明度影响动画

比如页面本身就是透明的情况下,还设置透明度动画,那么就会看上去无效。

七、插件化问题导致找不到动画

如果动画资源找不到,都会引起动画失效的问题。

插件化的场景中,比较特殊的地方是:

有些插件化框架加载动画资源,需要使用其框架对应的API来操作。

原因是:插件化框架一般都会更改资源的id,通过固定的API才能够找到对应的资源。

在部分插件化框架中,如果直接调用overridePendingTransition来加载动画,会无法找到动画资源,并且Android Studio也不会报错。

比如下面代码,直接在插件中调用就可能会找不到资源,并且Android Studio也不会报错。

overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有