Android Open Source - GalDroid Image View On Touch Listener






From Project

Back to project page GalDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project GalDroid 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.raptor2101.GalDroid.Activities.Listeners;
//  ww w .  j  ava 2s . c o m
import de.raptor2101.GalDroid.Activities.Views.GalleryImageView;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Gallery;

public class ImageViewOnTouchListener implements OnTouchListener {

  private enum TouchMode {
    None, Drag, Zoom,
  }

  private final Gallery mGalleryFullscreen;
  private final Gallery mGalleryThumbnails;

  private final float mMinDragHeight;

  private TouchMode mTouchMode;
  private PointF mScalePoint = new PointF();
  private float mTouchStartY, mTouchStartX, mOldDist = 1;

  public ImageViewOnTouchListener(Gallery galleryFullscreen, Gallery galleryThumbnails, float minDragHeight) {
    mGalleryFullscreen = galleryFullscreen;
    mGalleryThumbnails = galleryThumbnails;
    mMinDragHeight = minDragHeight;
  }

  public boolean onTouch(View v, MotionEvent event) {
    Log.d("ImageViewActivity", "EventAction: " + event.getAction());
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
      mTouchStartY = event.getY();
      mTouchStartX = event.getX();
      mTouchMode = TouchMode.Drag;
      Log.d("ImageViewActivity", "Start Drag");
      break;
    case MotionEvent.ACTION_POINTER_3_DOWN:
    case MotionEvent.ACTION_POINTER_2_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
      /*
       * mOldDist = getSpacing(event); if(mOldDist > 10f) { mTouchMode =
       * TouchMode.Zoom; setScalePoint(event); }
       */
      break;
    case MotionEvent.ACTION_POINTER_3_UP:
    case MotionEvent.ACTION_POINTER_2_UP:
    case MotionEvent.ACTION_POINTER_UP:
      mTouchMode = TouchMode.None;
      break;
    case MotionEvent.ACTION_UP:
      if (mTouchMode == TouchMode.Drag) {
        if (Math.abs(event.getX() - mTouchStartX) < 50) {
          float diffY = event.getY() - mTouchStartY;
          Log.d("ImageViewActivity", String.format("DragLength %.2f MinLength %.2f", Math.abs(diffY), mMinDragHeight));
          if (Math.abs(diffY) > mMinDragHeight) {
            if (diffY > 0 && mGalleryThumbnails.getVisibility() == View.VISIBLE) {
              mGalleryThumbnails.setVisibility(View.GONE);
            } else if (diffY < 0 && mGalleryThumbnails.getVisibility() == View.GONE) {
              mGalleryThumbnails.setVisibility(View.VISIBLE);
            }
          }
        }
      }
      break;
    case MotionEvent.ACTION_MOVE:
      if (mTouchMode == TouchMode.Zoom) {
        float dist = getSpacing(event);
        if (dist > 10f) {
          GalleryImageView imageView = (GalleryImageView) mGalleryFullscreen.getSelectedView();
          float scale = dist / mOldDist;
          if (scale >= 1 && scale <= 10) {
            Log.d("ImageViewActivity", "ACTION_MOVE Scale:" + scale);
            Matrix matrix = imageView.getImageMatrix();

            matrix.postScale(scale, scale, mScalePoint.x - imageView.getLeft(), mScalePoint.y - imageView.getTop());
            imageView.setImageMatrix(matrix);
          }
        }
      }
      break;
    }
    return false;
  }

  private float getSpacing(MotionEvent event) {
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    return FloatMath.sqrt(x * x + y * y);
  }
// Comment out till needed  
//  private void setScalePoint(MotionEvent event) {
//    float x = event.getX(0) + event.getX(1);
//    float y = event.getY(0) + event.getY(1);
//    mScalePoint.set(x / 2, y / 2);
//  }
}




Java Source Code List

de.raptor2101.GalDroid.Activities.EditGalleryActivity.java
de.raptor2101.GalDroid.Activities.GalDroidApp.java
de.raptor2101.GalDroid.Activities.GalleryActivity.java
de.raptor2101.GalDroid.Activities.GalleryListingActivitiy.java
de.raptor2101.GalDroid.Activities.GridViewActivity.java
de.raptor2101.GalDroid.Activities.ImageViewActivity.java
de.raptor2101.GalDroid.Activities.Helpers.ActionBarHider.java
de.raptor2101.GalDroid.Activities.Helpers.ImageAdapter.java
de.raptor2101.GalDroid.Activities.Listeners.ImageViewOnTouchListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageViewListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageView.java
de.raptor2101.GalDroid.Activities.Views.ImageInformationView.java
de.raptor2101.GalDroid.Config.GalDroidPreference.java
de.raptor2101.GalDroid.Config.GalleryConfig.java
de.raptor2101.GalDroid.WebGallery.DegMinSec.java
de.raptor2101.GalDroid.WebGallery.GalleryFactory.java
de.raptor2101.GalDroid.WebGallery.ImageCache.java
de.raptor2101.GalDroid.WebGallery.ImageInformation.java
de.raptor2101.GalDroid.WebGallery.Stream.java
de.raptor2101.GalDroid.WebGallery.TitleConfig.java
de.raptor2101.GalDroid.WebGallery.Gallery3.DownloadObject.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Gallery3Imp.java
de.raptor2101.GalDroid.WebGallery.Gallery3.ProgressListener.java
de.raptor2101.GalDroid.WebGallery.Gallery3.RestCall.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.AlbumEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.CommentEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.EntityFactory.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.Entity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.PictureEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Tasks.JSONArrayLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryProgressListener.java
de.raptor2101.GalDroid.WebGallery.Interfaces.WebGallery.java
de.raptor2101.GalDroid.WebGallery.Tasks.CacheTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.CleanUpCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryVerifyTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.Progress.java
de.raptor2101.GalDroid.WebGallery.Tasks.RepeatingTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.SyncronizeCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.TaskInterface.java