Android Left to Right slide animation

前端 未结 9 944
旧时难觅i
旧时难觅i 2020-11-22 08:18

I have three activities whose launch modes are single instance.
Using onfling(), I swing them left and right.

The problem is when I swipe right to

相关标签:
9条回答
  • 2020-11-22 08:59

    Made a sample code implementing the same with slide effects from left, right, top and bottom. (For those who dont want to make all those anim xml files :) )

    Checkout out the code on github

    0 讨论(0)
  • 2020-11-22 09:04

    You can overwrite your default activity animation. Here is the solution that I use:

    Create a "CustomActivityAnimation" and add this to your base Theme by "windowAnimationStyle".

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorPrimary</item>
        <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>
    
    </style>
    
    <style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
        <item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
        <item name="android:activityCloseEnterAnimation">@anim/slide_in_left</item>
        <item name="android:activityCloseExitAnimation">@anim/slide_out_right</item>
    </style>
    

    Create anim folder under res folder and then create this four animation files:

    slide_in_right.xml

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

    slide_out_left.xml

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

    slide_in_left.xml

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

    slide_out_right.xml

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

    This is my sample project in github.

    That's all... Happy coding :)

    0 讨论(0)
  • 2020-11-22 09:10

    I was not able to find any solution for this type of animation using ViewPropertyAnimator.

    Here's an example:

    Layout:

    <FrameLayout
    android:id="@+id/child_view_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/child_view"
            android:gravity="center_horizontal"
            android:layout_gravity="center_horizontal"
        />
    </FrameLayout>
    

    Animate - Right to left and exit view:

    final childView = findViewById(R.id.child_view);
    View containerView = findViewById(R.id.child_view_container);
    childView.animate()
      .translationXBy(-containerView.getWidth())
      .setDuration(TRANSLATION_DURATION)
      .setInterpolator(new AccelerateDecelerateInterpolator())
      .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            childView.setVisibility(View.GONE);
        }
    });
    

    Animate - Right to left enter view:

    final View childView = findViewById(R.id.child_view);
    View containerView = findViewById(R.id.child_view_container);
    childView.setTranslationX(containerView.getWidth());
    childView.animate()
        .translationXBy(-containerView.getWidth())
        .setDuration(TRANSLATION_DURATION)
        .setInterpolator(new AccelerateDecelerateInterpolator())
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                childView.setVisibility(View.VISIBLE);
            }
        });
    
    0 讨论(0)
提交回复
热议问题