Dynamic scale animation when replacing a fragment

I want to replace the fragment with animation, but it must be dynamic every time, i.e. it will start from the point where I will click on the screen, but the method fragmentTransaction.setCustomAnimationsuses a predefined animation defined in the animation folder:

fragmentTransaction.setCustomAnimations(R.anim.bounce, R.anim.bounce);

I create an object ScaleAnimationto satisfy my need:

ScaleAnimation animation = new ScaleAnimation(fromX,ToX,fromY,toY,pivitX,pivotY);
animation.setDuration(500);

fragmentTransaction.setCustomAnimationsthe method does not accept ScaleAnimation, it accepts only int. So, how to achieve dynamic animation when replacing a fragment.

+4
source share
1 answer

You can create your own sets of animations and use them.

XML "res/anim", :

fragmentTransaction.setCustomAnimations(R.anim.your_animation, R.anim.your_animation);

:

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

<translate
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:toXDelta="-10%p"
    android:toYDelta="1%p"/>

<scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.98"
    android:toYScale="0.98"/>

<translate
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXDelta="-10%p"
    android:fromYDelta="1%p"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:startOffset="@android:integer/config_shortAnimTime"
    android:toXDelta="100%p"
    android:toYDelta="5%p"/>

<scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="0.98"
    android:fromYScale="0.98"
    android:startOffset="@android:integer/config_shortAnimTime"
    android:toXScale="0.9"
    android:toYScale="0.9"/>
</set>
+1

Source: https://habr.com/ru/post/1619510/


All Articles