/OrigamiAnimationView.java Secret
Last active
August 29, 2015 14:17
-
-
Save mustafasevgi/d49c31467053e80a9012 to your computer and use it in GitHub Desktop.
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
package com.facebook.origami; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.FrameLayout; | |
import com.facebook.rebound.SimpleSpringListener; | |
import com.facebook.rebound.Spring; | |
import com.facebook.rebound.SpringConfig; | |
import com.facebook.rebound.SpringSystem; | |
import com.facebook.rebound.SpringUtil; | |
public class OrigamiAnimationView extends FrameLayout { | |
private final SpringSystem springSystem; | |
private final Spring photoZoomSpring; | |
private final Spring fadeSpring; | |
private final View story; | |
private final View photo; | |
public OrigamiAnimationView(Context context) { | |
this(context, null); | |
} | |
public OrigamiAnimationView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public OrigamiAnimationView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
// Hook up variables to your views here | |
story = null; | |
photo = null; | |
springSystem = SpringSystem.create(); | |
photoZoomSpring = springSystem.createSpring() | |
.setSpringConfig(SpringConfig.withBouncinessAndSpeed(7, 2)) | |
.addListener(new SimpleSpringListener() { | |
@Override | |
public void onSpringUpdate(Spring spring) { | |
setPhotoZoomProgress((float) spring.getCurrentValue()); | |
} | |
}); | |
fadeSpring = springSystem.createSpring() | |
.setSpringConfig(SpringConfig.withBouncinessAndSpeed(15, 2)) | |
.addListener(new SimpleSpringListener() { | |
@Override | |
public void onSpringUpdate(Spring spring) { | |
setFadeProgress((float) spring.getCurrentValue()); | |
} | |
}); | |
} | |
// photoZoom transition | |
public void photoZoom(boolean on) { | |
photoZoomSpring.setEndValue(on ? 1 : 0); | |
} | |
public void setPhotoZoomProgress(float progress) { | |
float alpha2 = transition(progress, 1f, 0f); | |
story.setOpacity(alpha2); | |
float scale2 = transition(progress, 0.4797f, 0.55f); | |
photo.setScaleX(scale2); | |
photo.setScaleY(scale2); | |
float yPosition2 = transition(progress, -186f, 0f); | |
photo.setTranslationY(yPosition2); | |
float scale2 = transition(progress, 1f, 0.97f); | |
story.setScaleX(scale2); | |
story.setScaleY(scale2); | |
float rotation2 = transition(progress, 0f, 30f); | |
photo.setRotation(rotation2); | |
} | |
// fade transition | |
public void fade(boolean on) { | |
fadeSpring.setEndValue(on ? 1 : 0); | |
} | |
public void setFadeProgress(float progress) { | |
float alpha2 = transition(progress, 1f, 0.3f); | |
photo.setOpacity(alpha2); | |
} | |
// Utilities | |
public float transition (float progress, float startValue, float endValue) { | |
return (float) SpringUtil.mapValueFromRangeToRange(progress, 0, 1, startValue, endValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment