Android image viewer custom view onMeasure

The android image viewer custom view onMeasure tutorial describes how to set the size of your image view.

If your image view settings have been done in the layout xml file then your height will have been defined. This size could either be MATCH_PARENT, WRAP_CONTENT or a specific size.

In our sample we have set the image view width & height to match parent. So the default dimension values in the onMeasure method will be set to the MATCH_PARENT values defined in the xml file.

We have the opportunity to override those values in the onMeasure method. In other words change the size of our image view.

Because of the nature of the pinching & zooming on the image view, its dimensions will change. So we will be providing a number of values depending on the zoomed state. The maximum size of the image view will equal MATCH_PARENT. And the minimum size of the image view will match the size of the scaled bitmap.

These values need to passed setMeasuredDimension() inside the onMeasure method.

This tutorial also covers setting the application to full screen immersive mode.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/android_image_viewer.git Tag

image-viewer-onmeasure

or you can run this command

git clone https://github.com/mobapptuts/android_image_viewer.git –branch

image-viewer-onmeasure

Code Samples

Set the app to full screen sticky immersive mode

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        View decorView = getWindow().getDecorView();
        if (hasFocus) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
    }

 Override the custom view onMeasure method

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int imageWidth = MeasureSpec.getSize(widthMeasureSpec);
        int imageHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(
                Math.min(imageWidth, mImageWidth),
                Math.min(imageHeight, mImageHeight)
        );
    }

Android image viewer custom view onMeasure summary

In the android image viewer custom view onMeasure tutorial we were introduced to the onMeasure method. This is the method to use to set the size of your custom image view.

So we used the onMeasure method to set a maximum of MATCH_PARENT and a minimum of the scaled bitmap depending on the scale factor value.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>