Android DialogFragment昏暗的背景动画无法在DialogFragment退出上正常工作

时间:2019-03-10 00:45:53

标签: android animation android-animation exit android-dialogfragment

我正在尝试将enter / exit动画以及昏暗的背景动画添加到DialogFragment中。输入和退出动画以及进入时的暗淡背景动画都可以正常工作。但是,退出DialogFragment时,背景昏暗的动画无法完全正常工作。

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvTotal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:maxLines="3"
        android:padding="10dp"
        android:scrollbars="vertical"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeMinTextSize="2sp"
        app:autoSizeStepGranularity="2sp"
        app:autoSizeTextType="uniform"
        app:fontFamily="sans-serif"
        android:lineSpacingMultiplier="2.5"
        android:lineSpacingExtra="6dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:contentDescription="@null"
        android:importantForAccessibility="no"
        app:srcCompat="@drawable/bitcoin" />

</LinearLayout>

activity_main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

        <item
            android:id="@+id/menu_item_info"
            android:icon="@drawable/info_outline"
            android:title="@string/details_menu_title"
            android:titleCondensed="@string/info_menu_title_condensed"
            app:showAsAction="ifRoom" />

</menu>

fragment_bitcoin_information_dialog_menu_layout_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/colorPrimaryLight" />
    <stroke android:width="5dp" android:color="@color/colorAccent" />
    <corners android:radius="25dp" />
    <!-- <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> -->

</shape>

fragment_bitcoin_information_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/fragment_bitcoin_information_dialog_menu_layout_background"
    android:orientation="vertical"
    android:padding="50dp"
    tools:context=".BitcoinInformationDialogFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/btc_price_label"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                android:textStyle="bold" />

            <View
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorAccent" />

            <TextView
                android:id="@+id/tvPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="45dp"
                android:gravity="center"
                android:text="@string/btc_in_circulation_label"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                android:textStyle="bold" />

            <View
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorAccent" />

            <TextView
                android:id="@+id/tvCirculation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="45dp"
                android:gravity="center"
                android:text="@string/my_net_worth_label"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                android:textStyle="bold" />

            <View
                android:layout_width="wrap_content"
                android:layout_height="5dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorAccent" />

            <TextView
                android:id="@+id/tvNet"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

anim / scale_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000" />

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="5000"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator" />

</set>

此代码有效。要完全看到背景,需要5秒(5000毫秒)的背景时间变暗到0.7的alpha值和5秒钟(DialogFragment的5000毫秒)才能完全可见。

anim / scale_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0"
        android:toYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000" />

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0"
        android:duration="5000"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"  />

</set>

此代码不起作用。 DialogFragment需要5秒钟(DialogFragment为5000毫秒)才能完全隐藏。但是,从0.7到1.0的alpha值的背景暗淡动画几乎立即发生。这是我的问题:如何使此动画持续5秒(5000毫秒)并与退出动画同时结束?

styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">

        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>

    <style name="DialogScale" parent="@android:style/Animation.Activity">

        <item name="android:windowEnterAnimation">@anim/scale_in</item>
        <item name="android:windowExitAnimation">@anim/scale_out</item>

    </style>

    <style name="DialogTheme" parent="@android:style/Theme.Dialog">

        <item name="android:windowAnimationStyle">@style/DialogScale</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowActionModeOverlay">false</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimAmount">0.7</item>

    </style>

</resources>

我觉得答案位于此文件中的某个位置。是吗?

MainActivity.java:

package com.shikhar_mainalee.iownallbitcoin;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

import static com.shikhar_mainalee.iownallbitcoin.Main.NUMBER_OF_CURRENT_BITCOINS_AS_STRING;
import static com.shikhar_mainalee.iownallbitcoin.Main.OnBitcoinInformationChangedListener;
import static com.shikhar_mainalee.iownallbitcoin.Main.mMainActivityOnBitcoinInformationChangedListener;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main_menu, menu);
        return true;

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.menu_item_info:
                BitcoinInformationDialogFragment.newInstance().show(getSupportFragmentManager(), "fragment_bitcoin_information_dialog");
                break;

        }

        return true;

    }

}

BitcoinInformationDialogFragment.java:

package com.shikhar_mainalee.iownallbitcoin;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import static com.shikhar_mainalee.iownallbitcoin.Main.OnBitcoinInformationChangedListener;
import static com.shikhar_mainalee.iownallbitcoin.Main.mBitcoinInformationDialogFragmentOnBitcoinInformationChangedListener;

public class BitcoinInformationDialogFragment extends DialogFragment {

    public BitcoinInformationDialogFragment() {
    }

    public static BitcoinInformationDialogFragment newInstance() {

        return new BitcoinInformationDialogFragment();

    }

    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
        setStyle(STYLE_NORMAL, R.style.DialogTheme);

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return inflater.inflate(R.layout.fragment_bitcoin_information_dialog, container);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

    }

}

如何解决此错误?

P.S。这是当前预览的链接。

https://drive.google.com/file/d/1JTJf_uZsxzGdiWKL_234hOQBubW_b4Eo/view?usp=sharing

编辑:我发现,在较新的API级别上,DialogFragment退出动画和暗淡的背景动画都需要5秒(5000毫秒),而在较旧的API级别上则不需要。如何使其也适用于旧版本?

0 个答案:

没有答案