get Framing Rect from Image rectangle - Android Graphics

Android examples for Graphics:Image Shape

Description

get Framing Rect from Image rectangle

Demo Code


//package com.java2s;

import android.graphics.Rect;

public class Main {
    public static Rect getFramingRect(int imgWidth, int imgHeight) {

        int width;
        int height;
        Rect framingRect;// w  w w . j  a va 2 s. c o m
        if (imgWidth > imgHeight) {
            if (imgWidth * 9 / 16 > imgHeight) {
                width = imgHeight * 16 / 9;
                height = imgHeight;
            } else {
                height = imgWidth * 9 / 16;
                width = imgWidth;
            }
        } else {
            if (imgWidth * 16 / 9 > imgHeight) {
                width = imgHeight * 9 / 16;
                height = imgHeight;

            } else {
                width = imgWidth;
                height = imgWidth * 16 / 9;
            }
        }
        int leftOffset = (imgWidth - width) / 2;
        int topOffset = (imgHeight - height) / 2;

        framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
                topOffset + height);

        return framingRect;
    }
}

Related Tutorials