Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LayoutAnimation support for Android RN
Reviewed By: dernienl Differential Revision: D2710141 fb-gh-sync-id: 28d6af84441b7c2dbc423b73eb05e71f62f7cdea
- Loading branch information
1 parent
cf892a9
commit 098fcb3
Showing
18 changed files
with
698 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...d/src/main/java/com/facebook/react/uimanager/layoutanimation/AbstractLayoutAnimation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.uimanager.layoutanimation; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
import android.view.View; | ||
import android.view.animation.AccelerateDecelerateInterpolator; | ||
import android.view.animation.AccelerateInterpolator; | ||
import android.view.animation.Animation; | ||
import android.view.animation.DecelerateInterpolator; | ||
import android.view.animation.Interpolator; | ||
import android.view.animation.LinearInterpolator; | ||
|
||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.common.MapBuilder; | ||
import com.facebook.react.uimanager.IllegalViewOperationException; | ||
|
||
/** | ||
* Class responsible for parsing and converting layout animation data into native {@link Animation} | ||
* in order to animate layout when a valid configuration has been supplied by the application. | ||
*/ | ||
/* package */ abstract class AbstractLayoutAnimation { | ||
|
||
// Forces animation to be playing 10x slower, used for debug purposes. | ||
private static final boolean SLOWDOWN_ANIMATION_MODE = false; | ||
|
||
abstract boolean isValid(); | ||
|
||
/** | ||
* Create an animation object for the current animation type, based on the view and final screen | ||
* coordinates. If the application-supplied configuraiton does not specify an animation definition | ||
* for this types, or if the animation definition is invalid, returns null. | ||
*/ | ||
abstract @Nullable Animation createAnimationImpl(View view, int x, int y, int width, int height); | ||
|
||
private static final Map<InterpolatorType, Interpolator> INTERPOLATOR = MapBuilder.of( | ||
InterpolatorType.LINEAR, new LinearInterpolator(), | ||
InterpolatorType.EASE_IN, new AccelerateInterpolator(), | ||
InterpolatorType.EASE_OUT, new DecelerateInterpolator(), | ||
InterpolatorType.EASE_IN_EASE_OUT, new AccelerateDecelerateInterpolator(), | ||
InterpolatorType.SPRING, new SimpleSpringInterpolator()); | ||
|
||
private @Nullable Interpolator mInterpolator; | ||
private int mDelayMs; | ||
|
||
protected @Nullable AnimatedPropertyType mAnimatedProperty; | ||
protected int mDurationMs; | ||
|
||
public void reset() { | ||
mAnimatedProperty = null; | ||
mDurationMs = 0; | ||
mDelayMs = 0; | ||
mInterpolator = null; | ||
} | ||
|
||
public void initializeFromConfig(ReadableMap data, int globalDuration) { | ||
mAnimatedProperty = data.hasKey("property") ? | ||
AnimatedPropertyType.fromString(data.getString("property")) : null; | ||
mDurationMs = data.hasKey("duration") ? data.getInt("duration") : globalDuration; | ||
mDelayMs = data.hasKey("delay") ? data.getInt("delay") : 0; | ||
mInterpolator = data.hasKey("type") ? | ||
getInterpolator(InterpolatorType.fromString(data.getString("type"))) : null; | ||
|
||
if (!isValid()) { | ||
throw new IllegalViewOperationException("Invalid layout animation : " + data); | ||
} | ||
} | ||
|
||
/** | ||
* Create an animation object to be used to animate the view, based on the animation config | ||
* supplied at initialization time and the new view position and size. | ||
* | ||
* @param view the view to create the animation for | ||
* @param x the new X position for the view | ||
* @param y the new Y position for the view | ||
* @param width the new width value for the view | ||
* @param height the new height value for the view | ||
*/ | ||
public final @Nullable Animation createAnimation( | ||
View view, | ||
int x, | ||
int y, | ||
int width, | ||
int height) { | ||
if (!isValid()) { | ||
return null; | ||
} | ||
Animation animation = createAnimationImpl(view, x, y, width, height); | ||
if (animation != null) { | ||
int slowdownFactor = SLOWDOWN_ANIMATION_MODE ? 10 : 1; | ||
animation.setDuration(mDurationMs * slowdownFactor); | ||
animation.setStartOffset(mDelayMs * slowdownFactor); | ||
animation.setInterpolator(mInterpolator); | ||
} | ||
return animation; | ||
} | ||
|
||
private static Interpolator getInterpolator(InterpolatorType type) { | ||
Interpolator interpolator = INTERPOLATOR.get(type); | ||
if (interpolator == null) { | ||
throw new IllegalArgumentException("Missing interpolator for type : " + type); | ||
} | ||
return interpolator; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...roid/src/main/java/com/facebook/react/uimanager/layoutanimation/AnimatedPropertyType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
package com.facebook.react.uimanager.layoutanimation; | ||
|
||
/** | ||
* Enum representing the different view properties that can be used when animating layout for | ||
* view creation. | ||
*/ | ||
/* package */ enum AnimatedPropertyType { | ||
OPACITY("opacity"), | ||
SCALE_XY("scaleXY"); | ||
|
||
private final String mName; | ||
|
||
private AnimatedPropertyType(String name) { | ||
mName = name; | ||
} | ||
|
||
public static AnimatedPropertyType fromString(String name) { | ||
for (AnimatedPropertyType property : AnimatedPropertyType.values()) { | ||
if (property.toString().equalsIgnoreCase(name)) { | ||
return property; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unsupported animated property : " + name); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return mName; | ||
} | ||
} |
Oops, something went wrong.