Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify programmatically overridePendingTransition animations

I want to start a new activity with a custom translate animation, so I use overridePendingTransition with my animation in xml.

overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left)

enter_from_right.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromXDelta="100%" android:toXDelta="0%"
    android:fromYDelta="0%" android:toYDelta="0%"
    android:duration="350" />

Is there any way to modify/create an animation programmatically for an activity transition? In this case, modify fromXDelta with an specific value dynamically

like image 901
moyo Avatar asked Oct 02 '17 08:10

moyo


1 Answers

I'll answer myself, after doing some research. You need two things:

1 - Create a "transparent" activity by setting the following theme in AndroidManifest. If you don't do this, a blank screen will appear:

<style name="TransparentActivity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

2. Create a TranslationAnimation (or the animation you want) manually, applying it to the root decorView, as well as disable the custom animations by setting them to 0:

        overridePendingTransition(0, 0);

        // Custom transition
        TranslateAnimation translateAnimation = new TranslateAnimation(-translationLength, 0, 0, 0);
        translateAnimation.setDuration(350);
        translateAnimation.setFillAfter(true);
        View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

        rootView.startAnimation(translateAnimation);

        rootView.getLayoutParams().width = translationLength;

If you want the reverse animation onBackPressed or when your activity finishes:

@Override
public void finish() {
        overridePendingTransition(0, 0);
        TranslateAnimation translateAnimation = new TranslateAnimation(0, -translationLength, 0, 0);
        translateAnimation.setDuration(350);
        translateAnimation.setFillAfter(true);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                YourActivity.super.finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        getWindow().getDecorView().findViewById(android.R.id.content).startAnimation(translateAnimation);
}
like image 85
moyo Avatar answered Nov 01 '22 06:11

moyo