Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpolites committed Feb 20, 2012
1 parent b3f97fd commit 2ac96f9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 70 deletions.
10 changes: 10 additions & 0 deletions main/src/com/polites/android/GestureImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class GestureImageView extends View {
private float maxScale = 5.0f;
private float minScale = 0.25f;

private float rotation = 0.0f;

private float centerX;
private float centerY;

Expand Down Expand Up @@ -148,6 +150,10 @@ protected void onDraw(Canvas canvas) {

canvas.translate(x, y);

if(rotation != 0.0f) {
canvas.rotate(rotation);
}

if(adjustedScale != 1.0f) {
canvas.scale(adjustedScale, adjustedScale);
}
Expand Down Expand Up @@ -257,6 +263,10 @@ public void reset() {
scaleAdjust = startingScale;
redraw();
}

public void setRotation(float rotation) {
this.rotation = rotation;
}

public void setGestureImageViewListener(GestureImageViewListener pinchImageViewListener) {
this.gestureImageViewListener = pinchImageViewListener;
Expand Down
54 changes: 29 additions & 25 deletions main/src/com/polites/android/GestureImageViewTouchListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public class GestureImageViewTouchListener implements OnTouchListener {

private GestureImageView image;

private PointF current = new PointF();
private PointF last = new PointF();
private PointF next = new PointF();
private final PointF current = new PointF();
private final PointF last = new PointF();
private final PointF next = new PointF();
private final PointF midpoint = new PointF();

private final VectorF scaleVector = new VectorF();
private final VectorF pinchVector = new VectorF();

boolean touched = false;

private float lastDistance;
private float initialDistance;
private float lastScale = 1.0f;
private float currentScale = 1.0f;

Expand Down Expand Up @@ -44,10 +48,6 @@ public class GestureImageViewTouchListener implements OnTouchListener {
private int imageWidth;
private int imageHeight;

private final PointF midpoint = new PointF();

private final VectorF lastVector = new VectorF();

private DoubleTapListener doubleTapListener;
private GestureDetector doubleTapDetector;
private GestureImageViewListener imageListener;
Expand Down Expand Up @@ -92,7 +92,7 @@ public GestureImageViewTouchListener(GestureImageView image, int displayWidth, i
public boolean onTouch(View v, MotionEvent event) {

if(doubleTapDetector.onTouchEvent(event)) {
lastDistance = 0;
initialDistance = 0;
lastScale = startingScale;
currentScale = startingScale;
next.x = image.getX();
Expand All @@ -103,7 +103,7 @@ public boolean onTouch(View v, MotionEvent event) {

multiTouch = false;

lastDistance = 0;
initialDistance = 0;
lastScale = currentScale;

if(!canDragX) {
Expand Down Expand Up @@ -144,13 +144,17 @@ else if(event.getAction() == MotionEvent.ACTION_DOWN) {
else if(event.getAction() == MotionEvent.ACTION_MOVE) {
if(event.getPointerCount() > 1) {
multiTouch = true;
if(lastDistance > 0) {
float distance = MathUtils.distance(event);
if(initialDistance > 0) {

pinchVector.set(event);
pinchVector.calculateLength();

float distance = pinchVector.length;

if(lastDistance != distance) {
if(initialDistance != distance) {

// We have moved (scaled)
currentScale = (distance / lastDistance) * lastScale;
currentScale = (distance / initialDistance) * lastScale;

if(currentScale > maxScale) {
currentScale = maxScale;
Expand All @@ -161,14 +165,14 @@ else if (currentScale < minScale) {

calculateBoundaries();

lastVector.length *= currentScale;
scaleVector.length *= currentScale;

lastVector.calculateEndPoint();
scaleVector.calculateEndPoint();

lastVector.length /= currentScale;
scaleVector.length /= currentScale;

next.x = lastVector.end.x;
next.y = lastVector.end.y;
next.x = scaleVector.end.x;
next.y = scaleVector.end.y;

image.setScale(currentScale);
image.setPosition(next.x, next.y);
Expand All @@ -182,17 +186,17 @@ else if (currentScale < minScale) {
}
}
else {
lastDistance = MathUtils.distance(event);
initialDistance = MathUtils.distance(event);

MathUtils.midpoint(event, midpoint);

lastVector.setStart(midpoint);
lastVector.setEnd(next);
scaleVector.setStart(midpoint);
scaleVector.setEnd(next);

lastVector.calculateLength();
lastVector.calculateAngle();
scaleVector.calculateLength();
scaleVector.calculateAngle();

lastVector.length /= lastScale;
scaleVector.length /= lastScale;
}
}
else {
Expand Down
9 changes: 0 additions & 9 deletions main/src/com/polites/android/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ public static float distance(MotionEvent event) {
return FloatMath.sqrt(x * x + y * y);
}

public static void vector(float x1, float y1, float x2, float y2, VectorF vector) {
vector.start.x = x1;
vector.start.y = y1;
vector.end.x = x2;
vector.end.y = y2;
vector.length = distance(x1, y1, x2, y2);
vector.angle = angle(x1, y1, x2, y2);
}

public static float distance(PointF p1, PointF p2) {
float x = p1.x - p2.x;
float y = p1.y - p2.y;
Expand Down
36 changes: 0 additions & 36 deletions main/src/com/polites/android/NullPointF.java

This file was deleted.

8 changes: 8 additions & 0 deletions main/src/com/polites/android/VectorF.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.polites.android;

import android.graphics.PointF;
import android.view.MotionEvent;

public class VectorF {

Expand All @@ -25,6 +26,13 @@ public void setEnd(PointF p) {
this.end.y = p.y;
}

public void set(MotionEvent event) {
this.start.x = event.getX(0);
this.start.y = event.getY(0);
this.end.x = event.getX(1);
this.end.y = event.getY(1);
}

public void calculateLength() {
length = MathUtils.distance(start, end);
}
Expand Down

0 comments on commit 2ac96f9

Please sign in to comment.