亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android activity動(dòng)畫不生效原因及解決方案總結(jié)

 更新時(shí)間:2021年11月03日 09:27:31   作者:許佳佳233  
android activity動(dòng)畫是一個(gè)比較簡單的功能。但是使用時(shí)總會(huì)由于各種小問題導(dǎo)致動(dòng)畫失效,筆者根據(jù)自己經(jīng)驗(yàn),整理了各種可能導(dǎo)致的原因,期望能對你有所幫助

activity動(dòng)畫方式

在AndroidMenifest中添加activity的動(dòng)畫屬性windowAnimationStyle

 <item name="android:windowAnimationStyle">@style/anim_fade</item>

在activity代碼中添加 overridePendingTransition

overridePendingTransition(int enterAnim,int exitAnim)

問題匯總

  • 一、動(dòng)畫寫的有問題
  • 二、activity theme中設(shè)置動(dòng)畫為null,或者parent theme設(shè)置動(dòng)畫為null
  • 三、overridePendingTransition 使用時(shí)機(jī)問題
  • 四、overridePendingTransition 寫錯(cuò)地方
  • 五、onPause與onResume中的overridePendingTransition會(huì)覆蓋其他位置
  • 六、透明度影響動(dòng)畫
  • 七、插件化問題導(dǎo)致找不到動(dòng)畫

一、動(dòng)畫寫的有問題

動(dòng)畫本身出問題的方式無法一一列舉,常見的有“duration設(shè)置為0”,“from與to的值設(shè)置相同”。

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromAlpha="0.0" android:toAlpha="0.0"
    android:duration="300" />
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromAlpha="0.0" android:toAlpha="1.0"
    android:duration="0" />

二、activity theme中設(shè)置動(dòng)畫為null,或者parent theme設(shè)置動(dòng)畫為null

如下:

    <style name="TestActivityTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>
<style name="TestActivityTheme" parent="ParentActivityTheme">
 
    </style>

    <style name="ParentActivityTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@null</item>
    </style>

三、overridePendingTransition 使用時(shí)機(jī)問題

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.

其中說了兩個(gè)overridePendingTransition 的使用時(shí)機(jī):

  • 在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 寫錯(cuò)地方

寫錯(cuò)地方就純屬是開發(fā)者的粗心,例子如下:

重寫了finish方法,但是調(diào)用的是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會(huì)覆蓋其他位置

根據(jù)筆者經(jīng)驗(yàn),onPause和onResume中如果寫了overridePendingTransition,那么其效果會(huì)覆蓋其他地方設(shè)置的動(dòng)畫。
比如你在finish的時(shí)候設(shè)置了overridePendingTransition,然后在onPause中也設(shè)置了overridePendingTransition,那么最終效果會(huì)以onPause中的。
比如下面的例子中,finish之后設(shè)置了動(dòng)畫,onPause中關(guān)閉了activity的動(dòng)畫,那么最終就是沒有動(dòng)畫。

    @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);
    }

六、透明度影響動(dòng)畫

比如頁面本身就是透明的情況下,還設(shè)置透明度動(dòng)畫,那么就會(huì)看上去無效。

七、插件化問題導(dǎo)致找不到動(dòng)畫

如果動(dòng)畫資源找不到,都會(huì)引起動(dòng)畫失效的問題。

插件化的場景中,比較特殊的地方是:

有些插件化框架加載動(dòng)畫資源,需要使用其框架對應(yīng)的API來操作。

原因是:插件化框架一般都會(huì)更改資源的id,通過固定的API才能夠找到對應(yīng)的資源。

在部分插件化框架中,如果直接調(diào)用overridePendingTransition來加載動(dòng)畫,會(huì)無法找到動(dòng)畫資源,并且Android Studio也不會(huì)報(bào)錯(cuò)。

比如下面代碼,直接在插件中調(diào)用就可能會(huì)找不到資源,并且Android Studio也不會(huì)報(bào)錯(cuò)。

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

到此這篇關(guān)于Android activity動(dòng)畫不生效原因及解決方案總結(jié)的文章就介紹到這了,更多相關(guān)Android activity動(dòng)畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論