Android Open Source - android-plotter Touch Handler






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

/*
 * Copyright 2013 serso aka se.solovyev/*from w  w  w  .  j  a  v  a2 s . c o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Contact details
 *
 * Email: se.solovyev@gmail.com
 * Site:  http://se.solovyev.org
 */

package org.solovyev.android.plotter;

import android.os.Build;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;

import javax.annotation.Nonnull;

final class TouchHandler implements View.OnTouchListener {

  @Nonnull
  private final VelocityTracker tracker = VelocityTracker.obtain();

  private boolean afterZoom;

  @Nonnull
  private final Listener listener;

  private TouchHandler(@Nonnull Listener listener) {
    this.listener = listener;
  }

  static TouchHandler create(@Nonnull Listener listener) {
    return new TouchHandler(listener);
  }

  @Override
  public boolean onTouch(@Nonnull View v, @Nonnull MotionEvent event) {
    return handleTouchEvent(event);
  }

  public boolean handleTouchEvent(@Nonnull MotionEvent event) {
    final float x = event.getX();
    final float y = event.getY();

    final int pointerCount = getPointerCount(event);
    switch (getAction(event)) {
      case MotionEvent.ACTION_DOWN:
        afterZoom = false;
        tracker.clear();
        tracker.addMovement(event);
        listener.onTouchDown(x, y);
        break;

      case MotionEvent.ACTION_MOVE:
        if (pointerCount == 1) {
          if (afterZoom) {
            tracker.clear();
            listener.onTouchDown(x, y);
            afterZoom = false;
          }
          tracker.addMovement(event);
          listener.onTouchMove(x, y);
        } else if (pointerCount == 2) {
          listener.onTouchZoomMove(x, y, getX(event, 1), getY(event, 1));
        }
        break;

      case MotionEvent.ACTION_UP:
        tracker.addMovement(event);
        tracker.computeCurrentVelocity(1000);
        listener.onTouchUp(x, y);
        break;

      case MotionEvent.ACTION_POINTER_DOWN:
        if (pointerCount == 2) {
          listener.onTouchZoomDown(x, y, getX(event, 1), getY(event, 1));
        }
        break;

      case MotionEvent.ACTION_POINTER_UP:
        if (pointerCount == 2) {
          listener.onTouchZoomUp(x, y, getX(event, 1), getY(event, 1));
          afterZoom = true;
        }
        break;
    }
    return true;
  }

  private int getAction(MotionEvent event) {
    return event.getAction() & MotionEvent.ACTION_MASK;
  }

  public float getXVelocity() {
    return tracker.getXVelocity();
  }

  public float getYVelocity() {
    return tracker.getYVelocity();
  }

  private static float getX(@Nonnull MotionEvent event, int pointer) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR ? event.getX(pointer) : 0;
  }

  private static float getY(@Nonnull MotionEvent event, int pointer) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR ? event.getY(pointer) : 0;
  }

  private static int getPointerCount(@Nonnull MotionEvent event) {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR ? event.getPointerCount() : 1;
  }

  static interface Listener {
    void onTouchDown(float x, float y);

    void onTouchMove(float x, float y);

    void onTouchUp(float x, float y);

    void onTouchZoomDown(float x1, float y1, float x2, float y2);

    void onTouchZoomMove(float x1, float y1, float x2, float y2);

    void onTouchZoomUp(float x1, float y1, float x2, float y2);
  }
}




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