最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

activity動畫方式

在AndroidMenifest中添加activity的動畫屬性windowAnimationStyle

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

在activity代碼中添加 overridePendingTransition

overridePendingTransition(int enterAnim,int exitAnim)

問題匯總

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

一、動畫寫的有問題

動畫本身出問題的方式無法一一列舉,常見的有“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è)置動畫為null,或者parent theme設(shè)置動畫為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 寫錯地方

寫錯地方就純屬是開發(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會覆蓋其他位置

根據(jù)筆者經(jīng)驗(yàn),onPause和onResume中如果寫了overridePendingTransition,那么其效果會覆蓋其他地方設(shè)置的動畫。
比如你在finish的時(shí)候設(shè)置了overridePendingTransition,然后在onPause中也設(shè)置了overridePendingTransition,那么最終效果會以onPause中的。
比如下面的例子中,finish之后設(shè)置了動畫,onPause中關(guān)閉了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);
    }

六、透明度影響動畫

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

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

如果動畫資源找不到,都會引起動畫失效的問題。

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

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

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

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

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

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

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

相關(guān)文章

  • android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例

    android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例

    下面小編就為大家?guī)硪黄猘ndroid popuwindow點(diǎn)擊外部窗口不消失的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android持久化保存cookie的方法

    Android持久化保存cookie的方法

    這篇文章主要介紹了Android持久化保存cookie的方法,在解析網(wǎng)頁信息的時(shí)候,需要登錄后才能訪問,所以使用httpclient模擬登錄,然后把cookie保存下來,以供下一次訪問使用,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android 開啟閃光燈做手電筒的詳解

    Android 開啟閃光燈做手電筒的詳解

    本篇文章是對Android中開啟閃光燈做手電筒的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • android?sharedUserId?使用知識盲點(diǎn)解析

    android?sharedUserId?使用知識盲點(diǎn)解析

    這篇文章主要為大家介紹了android?sharedUserId使用的知識盲點(diǎn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • android實(shí)現(xiàn)簡單的矩形裁剪框

    android實(shí)現(xiàn)簡單的矩形裁剪框

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡單的矩形裁剪框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android 多國語言value文件夾命名的方法

    Android 多國語言value文件夾命名的方法

    這篇文章主要介紹了Android 多國語言value文件夾命名的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android仿qq頂部消息欄效果

    Android仿qq頂部消息欄效果

    這篇文章主要介紹了Android仿qq頂部消息欄效果,需要的朋友可以參考下
    2018-04-04
  • Android事件分發(fā)機(jī)制?ViewGroup分析

    Android事件分發(fā)機(jī)制?ViewGroup分析

    這篇文章主要介紹了Android事件分發(fā)機(jī)制?ViewGroup分析,事件分發(fā)從手指觸摸屏幕開始,即產(chǎn)生了觸摸信息,被底層系統(tǒng)捕獲后會傳遞給Android的輸入系統(tǒng)服務(wù)IMS,更多相關(guān)介紹,需要的朋友可以參考一下
    2022-09-09
  • Flutter app頁面路由以及路由攔截的實(shí)現(xiàn)

    Flutter app頁面路由以及路由攔截的實(shí)現(xiàn)

    本篇介紹了介紹了Flutter如何使用路由來實(shí)現(xiàn)頁面的跳轉(zhuǎn),從而簡化頁面之間的耦合,并可以實(shí)現(xiàn)路由攔截。
    2021-06-06
  • Android中捕獲全局異常實(shí)現(xiàn)代碼

    Android中捕獲全局異常實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android中捕獲全局異常實(shí)現(xiàn)代碼,本文給出了2種方法,以及對應(yīng)實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-04-04

最新評論

错那县| 蒙阴县| 延边| 丹寨县| 宜阳县| 瓦房店市| 平安县| 冷水江市| 汽车| 广饶县| 洛南县| 清苑县| 桐乡市| 马鞍山市| 马山县| 内江市| 蓬溪县| 南丹县| 广南县| 米脂县| 娄底市| 金乡县| 曲靖市| 墨竹工卡县| 临武县| 东至县| 长阳| 彭阳县| 应城市| 安庆市| 灵石县| 鲁甸县| 永丰县| 竹北市| 安陆市| 兴隆县| 独山县| 扶余县| 海晏县| 南通市| 盐源县|