Android Open Source - android-plotter Zoomer






From Project

Back to project page android-plotter.

License

The source code is released under:

Apache License

If you think the Android project android-plotter 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 org.solovyev.android.plotter;
//from  ww  w  .  ja va  2 s  .  c o  m
import android.os.Bundle;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;

import javax.annotation.Nonnull;

import static android.os.SystemClock.uptimeMillis;

final class Zoomer {

  private static final long DURATION = 1000L;
  private static final float ZOOM_IN = .625f;
  private static final float ZOOM_OUT = 1.6f;

  @Nonnull
  private final Interpolator interpolator = new AccelerateDecelerateInterpolator();

  private float level = 1f;

  private float from = -1;
  private float to = level;

  private long startTime = -1;
  private long duration = DURATION;

  public Zoomer(@Nonnull Bundle bundle) {
    level = bundle.getFloat("zoom.level", 1f);
    to = level;
  }

  public Zoomer() {
  }

  public float getLevel() {
    return level;
  }

  /**
   * @return true if current zoom level has changed
   */
  public boolean onFrame() {
    if (isZooming()) {
      final long now = uptimeMillis();
      final float position = (now - startTime) / (float) duration;
      if (position >= 1f) {
        startTime = -1;
        from = -1;
        level = to;
        return true;
      }

      if (from > to) {
        level = from - (from - to) * interpolator.getInterpolation(position);
        return true;
      }

      if (from < to) {
        level = from + (to - from) * interpolator.getInterpolation(position);
        return true;
      }
    }

    return false;
  }

  public boolean zoom(boolean in) {
    if ((isZoomingIn() && in) || (isZoomingOut() && !in)) {
      return false;
    }

    if (in) {
      if (level * ZOOM_IN == 0f) {
        return false;
      }
      if (from == -1) {
        zoomTo(level * ZOOM_IN);
      } else {
        reverseZoom();
      }
    } else {
      if (level >= Float.MAX_VALUE / ZOOM_OUT) {
        return false;
      }
      if (from == -1) {
        zoomTo(level * ZOOM_OUT);
      } else {
        reverseZoom();
      }
    }

    return true;
  }

  public boolean zoomBy(float level) {
    if (isZooming()) {
      return false;
    }
    if (level == 1f) {
      return false;
    }
    zoomTo(this.level * level);
    duration = 1;
    return true;
  }

  private void zoomTo(float newLevel) {
    to = newLevel;
    from = level;
    duration = DURATION;
    startTime = uptimeMillis();
  }

  public boolean reset() {
    if (isZooming()) {
      if ((to == 1f) || (from == 1f)) {
        reverseZoom();
        return true;
      }
      return false;
    }

    if (level != 1) {
      zoomTo(1);
      return true;
    }

    return false;
  }

  private void reverseZoom() {
    final float tmp = to;
    to = from;
    from = tmp;

    final long now = uptimeMillis();
    final long remaining = duration - (now - startTime);
    startTime = now - remaining;
  }

  boolean isZooming() {
    return to != level;
  }

  boolean isZoomingIn() {
    return to < level;
  }

  boolean isZoomingOut() {
    return to > level;
  }

  public void saveState(@Nonnull Bundle bundle) {
    bundle.putFloat("zoom.level", isZooming() ? to : level);
  }

  @Override
  public String toString() {
    return "Zoomer{" +
        "level=" + (isZooming() ? to : level) +
        '}';
  }
}




Java Source Code List

com.android.texample.GLText.java
com.android.texample.SpriteBatch.java
com.android.texample.TexampleRenderer.java
com.android.texample.TextureRegion.java
com.android.texample.Vertices.java
org.solovyev.android.plotter.Angle.java
org.solovyev.android.plotter.AxisStyle.java
org.solovyev.android.plotter.Check.java
org.solovyev.android.plotter.Color.java
org.solovyev.android.plotter.DefaultPlotter.java
org.solovyev.android.plotter.Dimensions.java
org.solovyev.android.plotter.Frustum.java
org.solovyev.android.plotter.Function0.java
org.solovyev.android.plotter.Function1.java
org.solovyev.android.plotter.Function2.java
org.solovyev.android.plotter.Function.java
org.solovyev.android.plotter.LineStyle.java
org.solovyev.android.plotter.MeshConfig.java
org.solovyev.android.plotter.MultisampleConfigChooser.java
org.solovyev.android.plotter.PinchZoomTracker.java
org.solovyev.android.plotter.PlotData.java
org.solovyev.android.plotter.PlotFunction.java
org.solovyev.android.plotter.PlotRenderer.java
org.solovyev.android.plotter.PlotView.java
org.solovyev.android.plotter.Plot.java
org.solovyev.android.plotter.Plotter.java
org.solovyev.android.plotter.PlottingView.java
org.solovyev.android.plotter.Spf.java
org.solovyev.android.plotter.SuperFunction.java
org.solovyev.android.plotter.TouchHandler.java
org.solovyev.android.plotter.ZoomLevels.java
org.solovyev.android.plotter.Zoomer.java
org.solovyev.android.plotter.app.MainActivity.java
org.solovyev.android.plotter.app.PlotterApplication.java
org.solovyev.android.plotter.meshes.Arrays.java
org.solovyev.android.plotter.meshes.AxisGrid.java
org.solovyev.android.plotter.meshes.Axis.java
org.solovyev.android.plotter.meshes.BaseCube.java
org.solovyev.android.plotter.meshes.BaseCurve.java
org.solovyev.android.plotter.meshes.BaseMesh.java
org.solovyev.android.plotter.meshes.BaseSurface.java
org.solovyev.android.plotter.meshes.DimensionsAwareSwapper.java
org.solovyev.android.plotter.meshes.DimensionsAware.java
org.solovyev.android.plotter.meshes.DoubleBufferGroup.java
org.solovyev.android.plotter.meshes.DoubleBufferMesh.java
org.solovyev.android.plotter.meshes.FunctionGraph2d.java
org.solovyev.android.plotter.meshes.FunctionGraph3d.java
org.solovyev.android.plotter.meshes.FunctionGraphSwapper.java
org.solovyev.android.plotter.meshes.FunctionGraph.java
org.solovyev.android.plotter.meshes.Graph.java
org.solovyev.android.plotter.meshes.Group.java
org.solovyev.android.plotter.meshes.IndicesOrder.java
org.solovyev.android.plotter.meshes.ListGroup.java
org.solovyev.android.plotter.meshes.ListPool.java
org.solovyev.android.plotter.meshes.Mesh.java
org.solovyev.android.plotter.meshes.Meshes.java
org.solovyev.android.plotter.meshes.Pool.java
org.solovyev.android.plotter.meshes.Scene.java
org.solovyev.android.plotter.meshes.SolidCube.java
org.solovyev.android.plotter.meshes.SurfaceInitializer.java
org.solovyev.android.plotter.meshes.WireFrameCube.java