当我使用动画(slide_in和slide_out)时,屏幕消失之前消失

时间:2018-09-29 02:10:41

标签: android-fragments animation android-animation viewanimator

我正在使用视图动画在两个片段main_screen到login_screen之间进行动画过渡。它可以正常运行,但是我只有一个问题,当前屏幕会消失,然后消失,如下所示。

Error_gif

在这里您可以看到,当我转到另一个屏幕时,主屏幕的一半消失了。

这是我的动画的xml文件: 1)slide_in_left

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false"
    >
    <translate
        android:fillAfter="true"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
</set>

2)slide_in_right:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false"
    >
    <translate
        android:fillAfter="true"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

3)slide_out_left:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false"
    >
    <translate
        android:fillAfter="true"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

4)slide_out_right:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false"
    >
    <translate
        android:fillAfter="true"
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
</set>

这是出现问题的main_screen_xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddLocationFragment">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="116dp"
            android:background="@drawable/app_bar_background">

            <ImageView
                android:id="@+id/cover_logo"
                android:layout_width="160dp"
                android:layout_height="60dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/karomberlogo" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:textStyle="bold"
                android:id="@+id/header_text_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/header_str1"
                android:textAlignment="center"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/header_text_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/header_str2"
                android:textAlignment="center"
                android:textColor="@color/grey" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/attraction_img"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_attraction" />

            <ImageView
                android:id="@+id/hotel_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_hotel" />

            <ImageView
                android:id="@+id/resturant_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_restaurant" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/bank_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_bank" />

            <ImageView
                android:id="@+id/gas_station_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_gasstation" />

            <ImageView
                android:id="@+id/mobile_wallet_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_mobilewallet" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/rental_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_rental" />

            <ImageView
                android:id="@+id/tolit_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_toilet" />

            <ImageView
                android:id="@+id/shoping_mart_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_shoppingmart" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/workshop_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_workshop" />

            <ImageView
                android:id="@+id/hospital_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_hospital" />

            <ImageView
                android:id="@+id/police_station_img"
                android:layout_width="0dp"
                android:layout_height="120dp"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@drawable/tile_policestation" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp">

            <ImageView
                android:layout_width="237dp"
                android:layout_height="2dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:src="@drawable/divider" />
        </RelativeLayout>
    </LinearLayout>

</ScrollView>

这是login_screen_xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".ProfileFragment">

    <include
        android:id="@+id/include"
        layout="@layout/app_bar_new"
        app:layout_constraintBottom_toTopOf="@+id/tv_profile_desc"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_profile_desc"
        android:layout_width="279dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="179dp"
        android:text="@string/profile_desc"
        android:textAlignment="center"
        android:textColor="@color/blue"
        android:textSize="16sp"
        app:layout_constraintBottom_toTopOf="@+id/btn_signUp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/include" />

    <Button
        android:id="@+id/btn_signUp"
        style="@style/btn_style"
        android:layout_width="96dp"
        android:layout_height="40dp"
        android:layout_marginLeft="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="40dp"
        android:text="@string/btn_sign_up"
        app:layout_constraintEnd_toStartOf="@+id/btn_login"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_profile_desc" />

    <Button
        android:id="@+id/btn_login"
        style="@style/transparent_btn_style"
        android:layout_width="96dp"
        android:layout_height="40dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginTop="40dp"
        android:text="@string/btn_login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn_signUp"
        app:layout_constraintTop_toBottomOf="@+id/tv_profile_desc" />
</android.support.constraint.ConstraintLayout>

在这里是main_activity_xml,从中调用了两个fragemnts:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:id="@+id/root_view"
    >
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <android.support.design.widget.BottomNavigationView
        android:background="@color/clear_white"
        android:id="@+id/navBar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@color/blue_navbar_icon"
        app:itemTextColor="@color/blue_navbar_icon"
        app:menu="@menu/navigation" />
</RelativeLayout>

这是main_activity_java文件,我在其中为活动应用动画:

package com.example.eapple.tripdatacollection;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEventListener;

public class Main2Activity extends AppCompatActivity {

    private static AddLocationFragment addLocationFragment;
    private static ProfileFragment profileFragment;
    private static SavedLocationsFragment savedLocationsFragment;
    private gridViewFragment gridViewFragment;
    private View rootView;
    private final String TAG = "Main2Activity";

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            String tag;
            switch (item.getItemId()) {
                case R.id.navigation_profile:
                    tag = "profile_frag";
                    loadFragment(profileFragment, tag);
                    return true;
                case R.id.navigation_saved_locations:
                    tag = "saved_loc_frag";
                    loadFragment(savedLocationsFragment, tag);
                    return true;
                case R.id.navigation_add_location:
                    tag = "add_loc_frag";
                    //loadFragment(addLocationFragment, tag);
                    loadFragment(gridViewFragment, tag);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        addLocationFragment = new AddLocationFragment();
        savedLocationsFragment = new SavedLocationsFragment();
        profileFragment = new ProfileFragment();
        gridViewFragment = new gridViewFragment();

        rootView = findViewById(R.id.root_view);

        final BottomNavigationView navigation = findViewById(R.id.navBar);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setSelectedItemId(R.id.navigation_add_location);
        loadFragment(addLocationFragment, "add_loc_frag");

        KeyboardVisibilityEvent.setEventListener(
                this,
                new KeyboardVisibilityEventListener() {
                    @Override
                    public void onVisibilityChanged(boolean isOpen) {
                        Log.d(TAG,"onVisibilityChanged: Keyboard visibility changed");
                        if(isOpen){
                            Log.d(TAG, "onVisibilityChanged: Keyboard is open");
                            navigation.setVisibility(View.INVISIBLE);
                            Log.d(TAG, "onVisibilityChanged: NavBar got Invisible");
                        }else{
                            Log.d(TAG, "onVisibilityChanged: Keyboard is closed");
                            navigation.setVisibility(View.VISIBLE);
                            Log.d(TAG, "onVisibilityChanged: NavBar got Visible");
                        }
                    }
                });
    }

    private void loadFragment(android.support.v4.app.Fragment fragment, String tag) {
        int anim_left_id = R.anim.slide_in_right;
        int anim_right_id = R.anim.slide_out_left;
        if(getSupportFragmentManager().getBackStackEntryCount() > 0){
            android.support.v4.app.Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
            if(f instanceof ProfileFragment){
                anim_left_id = R.anim.slide_in_right;
                anim_right_id = R.anim.slide_out_left;
                //Log.d(TAG, "Profile Fragment");
            }else if(f instanceof SavedLocationsFragment){
                anim_left_id = R.anim.slide_in_left;
                anim_right_id = R.anim.slide_out_right;
                Log.d(TAG, "SavedLocations Fragment");
            }else if(f instanceof gridViewFragment){
                if(tag == "saved_loc_frag"){
                    anim_left_id = R.anim.slide_in_right;
                    anim_right_id = R.anim.slide_out_left;
                } else if(tag == "profile_frag"){
                    anim_left_id = R.anim.slide_in_left;
                    anim_right_id = R.anim.slide_out_right;
                }
                Log.d(TAG, "Add Locations Fragment");
            }
        }
        //Log.d(TAG, getCurrentFragment());
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(anim_left_id, anim_right_id);
        //fragmentTransaction.setTransition(R.id.)
        fragmentTransaction.replace(R.id.fragment_container, fragment,tag);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

}

0 个答案:

没有答案