Dynamic scale animation on replacing Fragment
Asked Answered
P

2

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.

Ponzo answered 11/12, 2015 at 14:8 Comment(1)
have you got answer?Sears
B
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>
Bischoff answered 14/12, 2015 at 20:55 Comment(2)
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 screenPonzo
This will lead to java.lang.RuntimeException: Unknown animator name: translate. See this post for further informations.Disharmony
D
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().

Disharmony answered 16/5, 2018 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.