Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Copied from https://github.com/ToxicBakery/ViewPagerTransforms
@SuppressWarnings("WeakerAccess")
public abstract class BaseTransformer implements PageTransformer {
public static final float DAMPING_RATIO_NO_BOUNCY = 1f;
/**
* Called each {@link #transformPage(View, float)}.
*
* @param view the view
* @param position the position
*/
protected abstract void onTransform(View view, float position);
@Override
public void transformPage(@NonNull View view, float position) {
onPreTransform(view, position);
onTransform(view, position);
onPostTransform(view, position);
}
/**
* If the position offset of a fragment is less than negative one or greater than one, returning true will set the
* visibility of the fragment to {@link View#GONE}. Returning false will force the fragment to {@link View#VISIBLE}.
*
* @return hideOffscreenPages
*/
protected boolean hideOffscreenPages() {
return true;
}
/**
* Indicates if the default animations of the view pager should be used.
*
* @return isPagingEnabled
*/
protected boolean isPagingEnabled() {
return false;
}
/**
* Called each {@link #transformPage(View, float)} before {{@link #onTransform(View, float)} is called.
*
* @param view the view
* @param position the position
*/
protected void onPreTransform(@NonNull View view, float position) {
float alpha = 0.0f;
float width = (float) view.getWidth();
view.setRotationX(0.0f);
view.setRotationY(0.0f);
view.setRotation(0.0f);
view.setScaleX(DAMPING_RATIO_NO_BOUNCY);
view.setScaleY(DAMPING_RATIO_NO_BOUNCY);
view.setPivotX(0.0f);
view.setPivotY(0.0f);
view.setTranslationY(0.0f);
view.setTranslationX(isPagingEnabled() ? 0.0f : (-width) * position);
if (hideOffscreenPages()) {
if (position > -1.0f && position < DAMPING_RATIO_NO_BOUNCY) {
alpha = DAMPING_RATIO_NO_BOUNCY;
}
view.setAlpha(alpha);
view.setEnabled(false);
return;
}
view.setEnabled(true);
view.setAlpha(DAMPING_RATIO_NO_BOUNCY);
}
/**
* Called each {@link #transformPage(View, float)} call after {@link #onTransform(View, float)} is finished.
*
* @param view the view
* @param position the position
*/
protected void onPostTransform(@NonNull View view, float position) {}
}
public class CubeTransformer extends BaseTransformer {
@Override
protected void onTransform(View view, float position) {
float rotationY = (float) mapValueFromRangeToRange((double) position, -1.0d, 1.0d, -90.0d, 90.0d);
view.setCameraDistance((float) dp(5600));
if (position > 0.0f) {
view.setRotationY(rotationY);
view.setPivotX(0.0f);
view.setPivotY(((float) view.getHeight()) * 0.5f);
} else if (position < 0.0f) {
view.setRotationY(rotationY);
view.setPivotX((float) view.getWidth());
view.setPivotY(((float) view.getHeight()) * 0.5f);
} else {
view.setRotationY(0.0f);
view.setPivotX(((float) view.getWidth()) * 0.5f);
view.setPivotY(((float) view.getHeight()) * 0.5f);
}
}
@Override
public boolean isPagingEnabled() {
return true;
}
@SuppressWarnings("SameParameterValue")
private static double mapValueFromRangeToRange(double value, double fromLow, double fromHigh, double toLow, double toHigh) {
return (((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow)) + toLow;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.