8

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.

fasteque
  • 4,309
  • 8
  • 38
  • 50
vishal sharma
  • 319
  • 4
  • 15

2 Answers2

2

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>
geNia
  • 985
  • 9
  • 20
  • 3
    This is possible i know but i want to achieve dynamic animation. I want to start animation from the point where i clicked on the screen – vishal sharma Dec 15 '15 at 05:06
  • This will lead to ```java.lang.RuntimeException: Unknown animator name: translate```. See [this](https://stackoverflow.com/q/4932462/4279747) post for further informations. – Mark Kowalski May 17 '18 at 07:45
2

Fragment animation with translate tag in XML will not work via fragmentTransaction.setCustomAnimations(). This will lead to java.lang.RuntimeException: Unknown animator name: translate. See this post for further informations.

Try to use objectAnimator instead. You can achieve a scale animation with an animator xml like the following:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="200dp" android:valueTo="0dp"
        android:valueType="floatType"
        android:propertyName="translationY"
        android:duration="@android:integer/config_longAnimTime" />
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0.0" android:valueTo="1.0"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="@android:integer/config_longAnimTime"/>
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0.5" android:valueTo="1.0"
        android:valueType="floatType"
        android:propertyName="scaleX"
        android:duration="@android:integer/config_longAnimTime"/>
    <objectAnimator
        android:interpolator="@android:interpolator/decelerate_quint"
        android:valueFrom="0.5" android:valueTo="1.0"
        android:valueType="floatType"
        android:propertyName="scaleY"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

The key value is android:propertyName where you have to set scaleX and/or scaleY. Now you can use this xml with fragmentTransaction.setCustomAnimations().

Mark Kowalski
  • 264
  • 2
  • 9