scale Rectangle To Clamp Size - Android Graphics

Android examples for Graphics:Rectangle

Description

scale Rectangle To Clamp Size

Demo Code


//package com.java2s;

import android.graphics.Rect;

public class Main {
    public static void scaleRectToClampSize(Rect rect, int sizeClamp) {
        final int origWidth = rect.width();
        final int origHeight = rect.height();

        if (origWidth > origHeight) {
            if (origWidth > sizeClamp) {
                final float ratio = (float) origHeight / origWidth;
                rect.right = rect.left + sizeClamp;
                rect.bottom = rect.top + (int) (sizeClamp * ratio);
            }//w  w w .  j  a  va 2s. co  m
        } else if (origWidth > origHeight) {
            if (origHeight > sizeClamp) {
                final float ratio = (float) origWidth / origHeight;
                rect.bottom = rect.top + sizeClamp;
                rect.right = rect.left + (int) (sizeClamp * ratio);
            }
        }
    }
}

Related Tutorials