Calculates the aspect ratio given a rectangle. - Android Graphics

Android examples for Graphics:Rectangle

Description

Calculates the aspect ratio given a rectangle.

Demo Code


//package com.java2s;
import android.graphics.Rect;

public class Main {
    /**/*from   w  w w  .  ja  v a 2s .  co  m*/
     * Calculates the aspect ratio given a rectangle.
     */
    public static float calculateAspectRatio(float left, float top,
            float right, float bottom) {

        final float width = right - left;
        final float height = bottom - top;
        final float aspectRatio = width / height;

        return aspectRatio;
    }

    /**
     * Calculates the aspect ratio given a rectangle.
     */
    public static float calculateAspectRatio(Rect rect) {

        final float aspectRatio = (float) rect.width()
                / (float) rect.height();

        return aspectRatio;
    }
}

Related Tutorials