jasonpolites/gesture-imageview

setScale & setPosition problems

gfowley opened this issue · 2 comments

Using setScale and setPosition seems to work initially (imageview shows image at specified scale and position), but any touch interaction results in imageview immediately reverting to default position and scale.

May be related to #27 ,but @silentw's fix:

protected void handleUp() {
  multiTouch = false;
  currentScale = image.getScale(); //THIS

...doesn't solve the problem for me.

After much mucking about with setScale and setPosition it seems the problem lays in the touch listener not being set to match the view position and scale, the touch listener contains state that is dependent upon the imageview state ?

Based upon their respective .reset() methods (which are more comprehensive than the setScale and setPosition methods) I've added .setTo(...) methods to GestureImageView...

public void reset() {
  x = centerX;
  y = centerY;
  scaleAdjust = startingScale;
  if (gestureImageViewTouchListener != null) {
    gestureImageViewTouchListener.reset();
  }
  redraw();
}

public void setTo(float newX, float newY, float newScale) {
  x = newX;
  y = newY;
  scaleAdjust = newScale;
  if (gestureImageViewTouchListener != null) {
      gestureImageViewTouchListener.setTo(newX, newY, newScale);
  }
  redraw();
}

...and GestureImageViewTouchListener...

public void reset() {
  currentScale = startingScale;
  next.x = centerX;
  next.y = centerY;
  calculateBoundaries();
  image.setScale(currentScale);
  image.setPosition(next.x, next.y);
  image.redraw();
}

public void setTo(float newX, float newY, float newScale) {
  currentScale = newScale;
  next.x = newX;
  next.y = newY;
  calculateBoundaries();
  image.setScale(currentScale);
  image.setPosition(next.x, next.y);
  image.redraw();
}

Using these methods results in the imagview displaying at the new scale and position. If I drag the image and then zoom it works as expected. However, if I just zoom the image (without having first panned) it reverts to the default scale and position.

Before I throw more hours at this, any advice on likely directions to proceed in ?

A few hours later...

Setting lastScale in GestureImageViewTouchListener seems to do the trick.

GestureImageView:

  public void setTo(float newX, float newY, float newScale) {
    x = newX;
    y = newY;
    scaleAdjust = newScale;
    if (gestureImageViewTouchListener != null) {
        gestureImageViewTouchListener.setTo(newX, newY, newScale);
    }
    redraw();
  }

GestureImageViewTouchListener:

  public void setTo(float newX, float newY, float newScale) {
    currentScale = newScale;
    lastScale = newScale;
    next.x = newX;
    next.y = newY;
    calculateBoundaries();
    image.setScale(currentScale);
    image.setPosition(next.x, next.y);
    image.redraw();
  }

Pull request to follow.

I was tempted to change setScale and setPostion to also work like this, but they are used internally in GestureImageView and I'd rather not cause breakage.

Leads me to the question, were setScale and setPosition only expected to work as used internally and I shouldn't have expected them to work as part of the the public API yet ?