Android Open Source - android-gesture-detectors Touch Activity






From Project

Back to project page android-gesture-detectors.

License

The source code is released under:

Android Gesture Detectors Framework Licence =================================== Copyright (c) 2012, Almer Thie All rights reserved. Redistribution and use in source and binary forms, with or withou...

If you think the Android project android-gesture-detectors 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 com.almeros.android.multitouch.sample;
/*from  w w w  .ja v  a  2s . c  om*/
import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import com.almeros.android.multitouch.MoveGestureDetector;
import com.almeros.android.multitouch.RotateGestureDetector;
import com.almeros.android.multitouch.ShoveGestureDetector;

/**
 * Test activity for testing the different GestureDetectors.
 * 
 * @author Almer Thie (code.almeros.com)
 * Copyright (c) 2013, Almer Thie (code.almeros.com)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
 *  in the documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 */
public class TouchActivity extends Activity implements OnTouchListener {

  private Matrix mMatrix = new Matrix();
    private float mScaleFactor = .4f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;  
    private int mAlpha = 255;
    private int mImageHeight, mImageWidth;

    private ScaleGestureDetector mScaleDetector;
    private RotateGestureDetector mRotateDetector;
    private MoveGestureDetector mMoveDetector;
    private ShoveGestureDetector mShoveDetector; 

  @SuppressWarnings("deprecation")
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Determine the center of the screen to center 'earth'
    Display display = getWindowManager().getDefaultDisplay();
    mFocusX = display.getWidth()/2f;
    mFocusY = display.getHeight()/2f;
    
    // Set this class as touchListener to the ImageView
    ImageView view = (ImageView) findViewById(R.id.imageView);
    view.setOnTouchListener(this);
    
    // Determine dimensions of 'earth' image
    Drawable d     = this.getResources().getDrawable(R.drawable.earth);
    mImageHeight   = d.getIntrinsicHeight();
    mImageWidth   = d.getIntrinsicWidth();

    // View is scaled and translated by matrix, so scale and translate initially
        float scaledImageCenterX = (mImageWidth*mScaleFactor)/2; 
        float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;
        
    mMatrix.postScale(mScaleFactor, mScaleFactor);
    mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
    view.setImageMatrix(mMatrix);

    // Setup Gesture Detectors
    mScaleDetector   = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
    mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
    mMoveDetector   = new MoveGestureDetector(getApplicationContext(), new MoveListener());
    mShoveDetector   = new ShoveGestureDetector(getApplicationContext(), new ShoveListener());
  }
  
  @SuppressWarnings("deprecation")
  public boolean onTouch(View v, MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        mRotateDetector.onTouchEvent(event);
        mMoveDetector.onTouchEvent(event);
        mShoveDetector.onTouchEvent(event);

        float scaledImageCenterX = (mImageWidth*mScaleFactor)/2;
        float scaledImageCenterY = (mImageHeight*mScaleFactor)/2;
        
        mMatrix.reset();
        mMatrix.postScale(mScaleFactor, mScaleFactor);
        mMatrix.postRotate(mRotationDegrees,  scaledImageCenterX, scaledImageCenterY);
        mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY - scaledImageCenterY);
        
    ImageView view = (ImageView) v;
    view.setImageMatrix(mMatrix);
    view.setAlpha(mAlpha);

    return true; // indicate event was handled
  }

  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
      mScaleFactor *= detector.getScaleFactor(); // scale change since previous event
      
      // Don't let the object get too small or too large.
      mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f)); 

      return true;
    }
  }
  
  private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
    @Override
    public boolean onRotate(RotateGestureDetector detector) {
      mRotationDegrees -= detector.getRotationDegreesDelta();
      return true;
    }
  }  
  
  private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
    @Override
    public boolean onMove(MoveGestureDetector detector) {
      PointF d = detector.getFocusDelta();
      mFocusX += d.x;
      mFocusY += d.y;    

      // mFocusX = detector.getFocusX();
      // mFocusY = detector.getFocusY();
      return true;
    }
  }    
  
  private class ShoveListener extends ShoveGestureDetector.SimpleOnShoveGestureListener {
    @Override
    public boolean onShove(ShoveGestureDetector detector) {
      mAlpha += detector.getShovePixelsDelta();
      if (mAlpha > 255)
        mAlpha = 255;
      else if (mAlpha < 0)
        mAlpha = 0;
      
      return true;
    }
  }  

}




Java Source Code List

com.almeros.android.multitouch.BaseGestureDetector.java
com.almeros.android.multitouch.MoveGestureDetector.java
com.almeros.android.multitouch.RotateGestureDetector.java
com.almeros.android.multitouch.ShoveGestureDetector.java
com.almeros.android.multitouch.TwoFingerGestureDetector.java
com.almeros.android.multitouch.sample.TouchActivity.java