Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

View does not re-layout if changing images #37

jcogilvie opened this Issue Aug 6, 2012 · 3 comments


None yet
4 participants

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);



        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():

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

...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 Sep 13, 2012

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

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

saimr commented Apr 6, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment