jasonpolites / gesture-imageview

Implements pinch-zoom, rotate, pan as an ImageView for Android 2.1+
1.15k stars 501 forks source link

View does not re-layout if changing images #37

Open jcogilvie opened 12 years ago

jcogilvie commented 12 years ago

I just updated to the latest version from your repository for the scaleType support, and it broke a couple of things:

1) if I set an image to a preview (e.g., a loading image) and then set it to a real image, the second image never shows up. I fixed this by forcing "layout = false" when the image changes. I also had to reset startingScale = -1.0f but for production you'd probably have to check if startingScale had been set in the xml or by your own code.

2) the orientation detection (landscape vs. portrait) isn't sufficient to cause the image to be fully on the screen; at least, not for scaleType = fitCenter. For example, I have a "landscape" image and a "landscape" phone, but the image's height (the shorter dimension) is slightly larger than my phone's height. However, due to the orientation checking in the GestureImageView, the image is fit based on its width, which causes the top and bottom to appear cut off, instead of having vertical, black bars on the left and right edges and showing me the whole image. I had this working before I adopted your latest changes; when it was working, my setupCanvas function was this:

protected void setupCanvas(int measuredWidth, int measuredHeight, int orientation) {

    if(lastOrientation != orientation) {
        layout = false;
        lastOrientation = orientation;
    }

    if(drawable != null && !layout) {
        int imageWidth = getImageWidth();
        int imageHeight = getImageHeight();

        hWidth = Math.round(((float)imageWidth / 2.0f));
        hHeight = Math.round(((float)imageHeight / 2.0f));

        if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
            displayHeight = measuredHeight;

            // Calc height based on width
            float ratio = (float) imageWidth / (float) imageHeight;

            displayWidth = Math.round( (float) displayHeight * ratio);

        }
        else {
            displayWidth = measuredWidth;

            // Calc height based on width
            float ratio = (float) imageHeight / (float) imageWidth;

            displayHeight = Math.round( (float) displayWidth * ratio) ;

        }

        startingScale = Math.min((float) measuredHeight / (float) imageHeight, (float) measuredWidth/ (float) imageWidth);

        scaleAdjust = startingScale;

        this.centerX = (float)measuredWidth / 2.0f;
        this.centerY = (float)measuredHeight / 2.0f;

        x = centerX;
        y = centerY;

        gestureImageViewTouchListener = new GestureImageViewTouchListener(this, measuredWidth, measuredHeight);
        gestureImageViewTouchListener.setMinScale(minScale * startingScale);
        gestureImageViewTouchListener.setMaxScale(maxScale * startingScale);

        drawable.setBounds(-hWidth,-hHeight,hWidth,hHeight);

        setOnTouchListener(gestureImageViewTouchListener);  

        layout = true;
    }
}

I think the line you care about is the one about math.min().

In the latest version, if I change the following in computeStartingScale():

   case CENTER_INSIDE:
            startingScale = Math.min((float) measuredHeight / (float) imageHeight, (float) measuredWidth/ (float) imageWidth);
            break;

...then it starts out scaled correctly, but if I zoom out, it resets to the no-vertical-bars zoom.

If I then take that value and assign it to both fitScaleHorizontal and fitScaleVertical (in effect, replacing them both with it--I called it fitScaleAbs) in both the view and the gesture listener, then it works as expected.

majcher commented 12 years ago

Confirming first point above. ImageView disapears when drawable/bitmap is changed with e.g. setImageBitmap().

daveganly commented 11 years ago

Did anyone ever manage to resolve this issue? I.e. when the image is changed with SetImageBitmap, the imageview dissapears?

saimr commented 11 years ago

@svt dragon....100/100 for second point. Was experiencing the same issue and ur soln worked like magic.