Android Open Source - slider Replayer






From Project

Back to project page slider.

License

The source code is released under:

Apache License

If you think the Android project slider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.devisnik.android.sliding;
//from ww w .j  a va 2  s .c  om
import android.os.Handler;
import de.devisnik.android.sliding.tile.TileShifter;
import de.devisnik.android.sliding.tile.TileStore;
import de.devisnik.sliding.IFrameListener;
import de.devisnik.sliding.IPiece;
import de.devisnik.sliding.IRobotFrame;
import de.devisnik.sliding.IShifterListener;
import de.devisnik.sliding.ShiftingEvent;

final class Replayer implements Runnable, IFrameListener {

  private final static String TAG = "Replayer";

  private final class Scrambler implements Runnable {
    @Override
    public void run() {
      itsAnimationTracker.inc();
      itsFrame.scramble();
      itsTileStore.get(itsFrame.getHole()).setHidden(true);
    }
  }

  private final class Resolver implements Runnable {
    @Override
    public void run() {
      itsAnimationTracker.inc();
      itsFrame.resolve();
    }
  }

  private class Rescheduler implements IShifterListener {
    @Override
    public void doneShifting() {
      if (itsRunning)
        reschedule();
    }
  }

  private class AnimationTracker implements IShifterListener {

    private int itsCounter;

    public void inc() {
      itsCounter++;
    }

    public boolean isAnimationRunning() {
      return itsCounter > 0;
    }

    @Override
    public void doneShifting() {
      itsCounter--;
      if (itsCounter == 0)
        reschedule();
    }
  }

  private final IRobotFrame itsFrame;
  private final Handler itsHandler = new Handler();
  private boolean itsRunning;
  private final ISpeed itsSpeed;
  private final Rescheduler itsRescheduler = new Rescheduler();
  private final AnimationTracker itsAnimationTracker = new AnimationTracker();
  private final Scrambler itsScrambler = new Scrambler();
  private final Resolver itsResolver = new Resolver();
  private final boolean itsPreview;
  private final TileStore itsTileStore;

  Replayer(final IRobotFrame frame, final TileStore tileStore, final ISpeed speed, final boolean preview) {
    itsFrame = frame;
    itsTileStore = tileStore;
    itsPreview = preview;
    itsFrame.addListener(this);
    itsSpeed = speed;
  }

  public void onClick() {
    if (!itsFrame.isResolved())
      post(itsResolver);
    else
      post(itsScrambler);
  }

  @Override
  public void run() {
    if (itsAnimationTracker.isAnimationRunning())
      return;
    if (!itsFrame.replayNext()) {
      itsTileStore.get(itsFrame.getHole()).setHidden(false);
      post(itsScrambler, itsPreview ? 1000 : itsSpeed.getWaitAfterSolved());
    }
  }

  private void post(final Runnable runnable, final int delayInMillis) {
    itsHandler.removeCallbacks(runnable);
    itsHandler.postDelayed(runnable, delayInMillis);
  }

  private void post(final Runnable runnable) {
    itsHandler.removeCallbacks(runnable);
    itsHandler.post(runnable);
  }

  public final void start() {
    Logger.d(TAG, "start");
    if (itsRunning)
      return;
    reschedule();
    itsRunning = true;
  }

  private void reschedule() {
    post(this);
  }

  public final void pause() {
    Logger.d(TAG, "pause");
    itsHandler.removeCallbacks(this);
    itsRunning = false;
  }

  @Override
  public void handleSwap(final IPiece left, final IPiece right) {
    // Logger.d(TAG, "handleSwap");
    TileShifter tileShifter = TileShifter.create(itsTileStore, itsHandler, itsSpeed.getShiftDuration(),
        itsRescheduler);
    tileShifter.animate(left, right).start();
  }

  @Override
  public void handleShifting(final ShiftingEvent[] events) {
    // Logger.d(TAG, "handleShifting");
    TileShifter tileShifter = TileShifter.create(itsTileStore, itsHandler, itsSpeed.getScrambleDuration(),
        itsAnimationTracker);
    tileShifter.animate(events).start();
  }
}




Java Source Code List

de.devisnik.android.sliding.ARandom.java
de.devisnik.android.sliding.FPSCounter.java
de.devisnik.android.sliding.FrameDrawer.java
de.devisnik.android.sliding.ISpeed.java
de.devisnik.android.sliding.ImageCache.java
de.devisnik.android.sliding.ImageFactory.java
de.devisnik.android.sliding.Logger.java
de.devisnik.android.sliding.OpenActivity.java
de.devisnik.android.sliding.OpenPreviewActivity.java
de.devisnik.android.sliding.PropertiesBuilder.java
de.devisnik.android.sliding.RenderHandler.java
de.devisnik.android.sliding.Replayer.java
de.devisnik.android.sliding.Settings.java
de.devisnik.android.sliding.SlidingPreferences.java
de.devisnik.android.sliding.SlidingWallpaper.java
de.devisnik.android.sliding.SpeedFactory.java
de.devisnik.android.sliding.SummaryUpdater.java
de.devisnik.android.sliding.tile.BitmapCachingPieceDrawer.java
de.devisnik.android.sliding.tile.CachingPieceDrawer.java
de.devisnik.android.sliding.tile.IPieceDrawer.java
de.devisnik.android.sliding.tile.ImagePieceDrawer.java
de.devisnik.android.sliding.tile.NumberPieceDrawer.java
de.devisnik.android.sliding.tile.PieceDrawerFactory.java
de.devisnik.android.sliding.tile.TileShifter.java
de.devisnik.android.sliding.tile.TileStore.java
de.devisnik.android.sliding.tile.Tile.java
de.devisnik.sliding.FrameFactory.java
de.devisnik.sliding.FrameScrambler.java
de.devisnik.sliding.FromHomeShiftingEvent.java
de.devisnik.sliding.IFrameListener.java
de.devisnik.sliding.IFrame.java
de.devisnik.sliding.IHole.java
de.devisnik.sliding.IMove.java
de.devisnik.sliding.IPiece.java
de.devisnik.sliding.IRandom.java
de.devisnik.sliding.IRobotFrame.java
de.devisnik.sliding.IShifterListener.java
de.devisnik.sliding.MoveFactory.java
de.devisnik.sliding.Point.java
de.devisnik.sliding.ShiftingEvent.java
de.devisnik.sliding.ToHomeShiftingEvent.java
de.devisnik.sliding.animation.AccelerateDecelerateInterpolator.java
de.devisnik.sliding.animation.AnimationRunner.java
de.devisnik.sliding.animation.Animation.java
de.devisnik.sliding.animation.IInterpolator.java
de.devisnik.sliding.animation.IMovement.java
de.devisnik.sliding.animation.IShiftable.java
de.devisnik.sliding.animation.LinearInterpolator.java
de.devisnik.sliding.animation.MetaShiftable.java
de.devisnik.sliding.animation.ShiftAnimation.java
de.devisnik.sliding.animation.Translation.java
de.devisnik.sliding.impl.Frame.java
de.devisnik.sliding.impl.Hole.java
de.devisnik.sliding.impl.Move.java
de.devisnik.sliding.impl.PieceIterator.java
de.devisnik.sliding.impl.Piece.java
de.devisnik.sliding.impl.RobotFrame.java
de.devisnik.sliding.impl.Scrambler.java