Skip to content

Commit

Permalink
Merge pull request #6 from ogaclejapan/loop
Browse files Browse the repository at this point in the history
Cancel processing and animation loop
  • Loading branch information
elevenetc committed Dec 6, 2015
2 parents d08dea9 + a71a875 commit 6ecd2d0
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package su.levenetc.android.textsurface.sample.checks;

import su.levenetc.android.textsurface.Text;
import su.levenetc.android.textsurface.TextBuilder;
import su.levenetc.android.textsurface.TextSurface;
import su.levenetc.android.textsurface.animations.Alpha;
import su.levenetc.android.textsurface.animations.AnimationsSet;
import su.levenetc.android.textsurface.animations.Circle;
import su.levenetc.android.textsurface.animations.Delay;
import su.levenetc.android.textsurface.animations.Loop;
import su.levenetc.android.textsurface.animations.Rotate3D;
import su.levenetc.android.textsurface.animations.ShapeReveal;
import su.levenetc.android.textsurface.animations.SideCut;
import su.levenetc.android.textsurface.animations.Slide;
import su.levenetc.android.textsurface.animations.TransSurface;
import su.levenetc.android.textsurface.contants.Align;
import su.levenetc.android.textsurface.contants.Axis;
import su.levenetc.android.textsurface.contants.Direction;
import su.levenetc.android.textsurface.contants.Pivot;
import su.levenetc.android.textsurface.contants.Side;
import su.levenetc.android.textsurface.contants.TYPE;

/**
* Created by Eugene Levenetc.
*/
public class ShapeRevealLoopSample {
public static void play(TextSurface textSurface) {

Text textA = TextBuilder.create("Now why you loer en kyk gelyk?").setPosition(Align.SURFACE_CENTER).build();
Text textB = TextBuilder.create("Is ek miskien van goud gemake?").setPosition(Align.BOTTOM_OF | Align.CENTER_OF, textA).build();
Text textC = TextBuilder.create("You want to fight, you come tonight.").setPosition(Align.BOTTOM_OF | Align.CENTER_OF, textB).build();
Text textD = TextBuilder.create("Ek moer jou sleg! So jy hardloop weg.").setPosition(Align.BOTTOM_OF | Align.CENTER_OF, textC).build();

final int flash = 1500;

textSurface.play(
new Loop(
Rotate3D.showFromCenter(textA, 500, Direction.CLOCK, Axis.X),
new AnimationsSet(TYPE.PARALLEL,
ShapeReveal.create(textA, flash, SideCut.hide(Side.LEFT), false),
new AnimationsSet(TYPE.SEQUENTIAL, Delay.duration(flash / 4), ShapeReveal.create(textA, flash, SideCut.show(Side.LEFT), false))
),
new AnimationsSet(TYPE.PARALLEL,
Rotate3D.showFromSide(textB, 500, Pivot.TOP),
new TransSurface(500, textB, Pivot.CENTER)
),
Delay.duration(500),
new AnimationsSet(TYPE.PARALLEL,
Slide.showFrom(Side.TOP, textC, 500),
new TransSurface(1000, textC, Pivot.CENTER)
),
Delay.duration(500),
new AnimationsSet(TYPE.PARALLEL,
ShapeReveal.create(textD, 500, Circle.show(Side.CENTER, Direction.OUT), false),
new TransSurface(1500, textD, Pivot.CENTER)
),
Delay.duration(500),
new AnimationsSet(TYPE.PARALLEL,
new AnimationsSet(TYPE.PARALLEL, Alpha.hide(textD, 700), ShapeReveal.create(textD, 1000, SideCut.hide(Side.LEFT), true)),
new AnimationsSet(TYPE.SEQUENTIAL, Delay.duration(500), new AnimationsSet(TYPE.PARALLEL, Alpha.hide(textC, 700), ShapeReveal.create(textC, 1000, SideCut.hide(Side.LEFT), true))),
new AnimationsSet(TYPE.SEQUENTIAL, Delay.duration(1000), new AnimationsSet(TYPE.PARALLEL, Alpha.hide(textB, 700), ShapeReveal.create(textB, 1000, SideCut.hide(Side.LEFT), true))),
new AnimationsSet(TYPE.SEQUENTIAL, Delay.duration(1500), new AnimationsSet(TYPE.PARALLEL, Alpha.hide(textA, 700), ShapeReveal.create(textA, 1000, SideCut.hide(Side.LEFT), true))),
new TransSurface(4000, textA, Pivot.CENTER)
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TextSurface extends FrameLayout {

private TreeSet<Text> textsTree = new TreeSet<>();
private SurfaceCamera camera = new SurfaceCamera();
private ISurfaceAnimation currentAnimation = null;

public TextSurface(Context context) {
super(context);
Expand Down Expand Up @@ -54,7 +55,8 @@ public void play(ISurfaceAnimation animation) {
configAnimations(animation);
animation.setTextSurface(this);
layout();
animation.start(null);
currentAnimation = animation;
currentAnimation.start(null);
}

private void layout() {
Expand Down Expand Up @@ -87,6 +89,10 @@ private void configAnimations(ISurfaceAnimation animation) {
}

public void reset() {
if (currentAnimation != null) {
currentAnimation.cancel();
currentAnimation = null;
}
textsTree.clear();
camera.reset();
invalidate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public AbstractSurfaceAnimation(Text text, int duration) {
return duration;
}

@Override public void cancel() {

}

@Override public void onAnimationUpdate(ValueAnimator animation) {
textSurface.invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Alpha extends AbstractSurfaceAnimation {

private int to;
private int from;
private ValueAnimator animator;

public static Alpha hide(Text text, int duration) {
return new Alpha(text, duration, 255, 0);
Expand All @@ -31,7 +32,7 @@ public Alpha(Text text, int duration, int from, int to) {
}

@Override public void start(@Nullable final IEndListener listener) {
ValueAnimator animator = ValueAnimator.ofInt(from, to);
animator = ValueAnimator.ofInt(from, to);
animator.setDuration(duration);
animator.addUpdateListener(this);

Expand All @@ -48,4 +49,11 @@ public Alpha(Text text, int duration, int from, int to) {
super.onAnimationUpdate(animation);
text.setAlpha((int) animation.getAnimatedValue());
}

@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AnimationsSet implements IEndListener, ISet {
private ISurfaceAnimation lastAnimation;//type == PARALLEL
private int index;
private IEndListener listener;
private boolean canceled;
private static final DurationComparator DURATION_COMPARATOR = new DurationComparator();

public AnimationsSet(TYPE type, ISurfaceAnimation... animations) {
Expand All @@ -38,6 +39,11 @@ public AnimationsSet(TYPE type, ISurfaceAnimation... animations) {
@Override public void onAnimationEnd(ISurfaceAnimation animation) {
if (Debug.ENABLED) Log.i("endAnimation", animation.toString());

if (canceled) {
finalizeAnimation();
return;
}

if (type == TYPE.SEQUENTIAL) {
if (index < animations.size()) {
setCurrentAnimation(animations.get(index++));
Expand All @@ -60,6 +66,8 @@ private void finalizeAnimation() {

@Override public void start(IEndListener listener) {
this.listener = listener;
index = 0;
canceled = false;

if (type == TYPE.SEQUENTIAL) {
setCurrentAnimation(animations.get(index++));
Expand All @@ -68,11 +76,7 @@ private void finalizeAnimation() {
Collections.sort(animations, DURATION_COMPARATOR);
lastAnimation = animations.getLast();

Iterator<ISurfaceAnimation> iterator = animations.iterator();
while (iterator.hasNext()) {
setCurrentAnimation(iterator.next());
iterator.remove();
}
for (ISurfaceAnimation animation : animations) setCurrentAnimation(animation);
}
}

Expand Down Expand Up @@ -103,6 +107,17 @@ private void setCurrentAnimation(ISurfaceAnimation animation) {
return 0;
}

@Override public void cancel() {
canceled = true;
if (type == TYPE.SEQUENTIAL) {
if (currentAnimation != null) {
currentAnimation.cancel();
}
} else {
for (ISurfaceAnimation animation : animations) animation.cancel();
}
}

@Override public LinkedList<ISurfaceAnimation> getAnimations() {
return animations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CamRot implements ICameraAnimation, ValueAnimator.AnimatorUpdateLis
private SurfaceCamera camera;
private Text textPivot;
private int pivotAlign;
private ObjectAnimator animator;

public CamRot(int duration, float rotationDelta) {
this.duration = duration;
Expand Down Expand Up @@ -57,14 +58,15 @@ public CamRot(int duration, float rotationDelta, Text textPivot, int pivotAlign)
setPivot();
float from = camera.getRotation();
float to = camera.getRotation() + rotationDelta;
ObjectAnimator animator = ObjectAnimator.ofFloat(camera, "rotation", from, to);
animator = ObjectAnimator.ofFloat(camera, "rotation", from, to);
animator.setDuration(duration);
animator.addUpdateListener(this);
animator.addListener(new AnimatorEndListener() {
@Override public void onAnimationEnd(Animator animation) {
if (listener != null) listener.onAnimationEnd(CamRot.this);
}
});

animator.start();
}

Expand Down Expand Up @@ -138,6 +140,13 @@ private static void rotatePoint(PointF point, PointF center, float angle) {
return duration;
}

@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}

@Override public void onAnimationUpdate(ValueAnimator animation) {
textSurface.invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ChangeColor extends AbstractSurfaceAnimation {
final float[] fromTriplet = new float[3];
final float[] toTriplet = new float[3];
final float[] hsv = new float[3];
private ValueAnimator animator;

public static ChangeColor fromTo(Text text, int duration, int from, int to) {
return new ChangeColor(text, duration, from, to);
Expand All @@ -41,7 +42,7 @@ public ChangeColor(Text text, int duration, int from, int to) {
Color.colorToHSV(from, fromTriplet);
Color.colorToHSV(to, toTriplet);

ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(duration);
animator.addUpdateListener(this);

Expand All @@ -62,4 +63,12 @@ public ChangeColor(Text text, int duration, int from, int to) {
text.getPaint().setColor(Color.HSVToColor(hsv));
super.onAnimationUpdate(animation);
}

@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@ public static Delay duration(int duration) {
return new Delay(duration);
}

private Runnable action = null;

public Delay(int duration) {
super(null, duration);
}

@Override public void start(@Nullable final IEndListener listener) {
textSurface.postDelayed(new Runnable() {
action = new Runnable() {
@Override public void run() {
if (listener != null) listener.onAnimationEnd(Delay.this);
}
}, duration);
};
textSurface.postDelayed(action, duration);
}

@Override public void cancel() {
if (action != null) {
textSurface.removeCallbacks(action);
action = null;
}
}

@Override public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package su.levenetc.android.textsurface.animations;

import su.levenetc.android.textsurface.interfaces.IEndListener;
import su.levenetc.android.textsurface.interfaces.ISurfaceAnimation;

/**
* Created by ogaclejapan.
*/
public class Loop extends Sequential {

private final IEndListener restartListener = new IEndListener() {
@Override public void onAnimationEnd(ISurfaceAnimation animation) {
if (canceled) {
if (endListener != null) endListener.onAnimationEnd(Loop.this);
} else {
Loop.super.start(restartListener);
}
}
};

private IEndListener endListener;
private boolean canceled;

public Loop(ISurfaceAnimation... animations) {
super(animations);
}

@Override public void start(IEndListener listener) {
endListener = listener;
canceled = false;
super.start(restartListener);
}

@Override public void cancel() {
canceled = true;
super.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package su.levenetc.android.textsurface.animations;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class Rotate3D implements ITextEffect, ValueAnimator.AnimatorUpdateListen
private int direction;
private int axis;
private boolean show;
private ObjectAnimator animator;

public static Rotate3D showFromSide(Text text, int duration, int pivot) {
return new Rotate3D(text, duration, pivot, 0, 0, true);
Expand Down Expand Up @@ -107,7 +109,6 @@ private Rotate3D(


PropertyValuesHolder valHolder = null;
ObjectAnimator animator;

int fromDegree;
int toDegree;
Expand Down Expand Up @@ -172,6 +173,7 @@ private Rotate3D(
animator.setDuration(duration);
animator.addUpdateListener(this);
animator.start();

} else {
throw new RuntimeException(getClass().getSuperclass() + " was not configured properly. Pivot:" + pivot);
}
Expand All @@ -187,6 +189,13 @@ private Rotate3D(
return duration;
}

@Override public void cancel() {
if (animator != null && animator.isRunning()) {
animator.cancel();
animator = null;
}
}

public void setRotationX(float rotationX) {
this.rotationX = rotationX;
}
Expand Down

0 comments on commit 6ecd2d0

Please sign in to comment.