Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic scale animation on replacing Fragment

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

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

I create object of ScaleAnimation to meet my need like this:

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

fragmentTransaction.setCustomAnimations method does not accept scaleAnimation it only accepts int. So how to attain dynamic animation while replacing fragment.

like image 232
vishal sharma Avatar asked Dec 11 '15 14:12

vishal sharma


People also ask

How do you animate fragment transitions?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.

How do you animate a fragment?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.


1 Answers

You can create custom animation sets and use them.

Create an .xml file and put it in 'res/anim' folder and then use its resource id in code:

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

Here is and example of the 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>
like image 92
geNia Avatar answered Sep 25 '22 02:09

geNia