Photoview: How to show initially full image with PhotoView?

Created on 17 Apr 2017  路  9Comments  路  Source: Baseflow/PhotoView

Hi guys!
First at all, thank you very much for your awesome library! Great job!

About my problem: when my Activity starts, I want to show initially full image in PhotoView widget (like center_crop scale). It's just for initialization, so I can't set scale_type = center_crop, because after that I can't see complete image when I pinch / zoom.

thank you very much for your help!

Most helpful comment

Yes:

public void showFullScreen() {
        float photoViewWidth = mPhotoView.getWidth();
        float photoViewHeight = mPhotoView.getHeight();

        float viewScale = mPhotoView.getScale();
        RectF rect = mPhotoView.getDisplayRect();

        // Compute initial base rect
        float baseRectWidth = (rect.right - rect.left) / viewScale;
        float baseRectHeight = (rect.bottom - rect.top) / viewScale;

        // Compute medium scale for full size
        double mediumScale, currentScale;
        if (baseRectHeight > baseRectWidth) {
            mediumScale = photoViewWidth / baseRectWidth;
        } else {
            mediumScale = photoViewHeight / baseRectHeight;
        }

        mediumScale = Math.round(mediumScale * 100.0) / 100.0;
        currentScale = Math.round(mPhotoView.getScale() * 100.0) / 100.0;

        // Apply new scale: minimum or medium
        if (currentScale < mediumScale) {
            mPhotoView.setScale((float) mediumScale);

        } else {
            mPhotoView.setScale(mPhotoView.getMinimumScale());
        }
    }

All 9 comments

Did you find a solution?

Looking for this feature as well

Zoom out feature will be good.

Did you find a solution?

Yes:

public void showFullScreen() {
        float photoViewWidth = mPhotoView.getWidth();
        float photoViewHeight = mPhotoView.getHeight();

        float viewScale = mPhotoView.getScale();
        RectF rect = mPhotoView.getDisplayRect();

        // Compute initial base rect
        float baseRectWidth = (rect.right - rect.left) / viewScale;
        float baseRectHeight = (rect.bottom - rect.top) / viewScale;

        // Compute medium scale for full size
        double mediumScale, currentScale;
        if (baseRectHeight > baseRectWidth) {
            mediumScale = photoViewWidth / baseRectWidth;
        } else {
            mediumScale = photoViewHeight / baseRectHeight;
        }

        mediumScale = Math.round(mediumScale * 100.0) / 100.0;
        currentScale = Math.round(mPhotoView.getScale() * 100.0) / 100.0;

        // Apply new scale: minimum or medium
        if (currentScale < mediumScale) {
            mPhotoView.setScale((float) mediumScale);

        } else {
            mPhotoView.setScale(mPhotoView.getMinimumScale());
        }
    }

@anthony3444 I tried your solution but got nothing. When I call photoView.setScale(something) nothing changes. Do you know why?

@giusecapo if this is not working for you try setting the scale AFTER the view renders:

Example:

    dashboard_image = (PhotoView) TheView.findViewById(R.id.dashboard_image);

    ViewTreeObserver vto = dashboard_image.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            dashboard_image.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            dashboard_image.setScale(2f);
        }
    });

@anthony3444
solution works more or less. but sometimes causes throwing :

java.lang.IllegalArgumentException: Medium zoom has to be less than Maximum zoom. Call setMaximumZoom() with a more appropriate value

just add this in your xml photoview

android:adjustViewBounds="true" android:scaleType="fitXY"

Was this page helpful?
0 / 5 - 0 ratings