scale Center Crop Rectangle - Android Graphics

Android examples for Graphics:Rectangle

Description

scale Center Crop Rectangle

Demo Code


//package com.java2s;

import android.graphics.Rect;

public class Main {
    public static Rect scaleCenterCrop(Rect src, Rect dest) {
        float scaleWidthRatio = (float) src.width() / dest.width();
        float scaleHeightRatio = (float) src.height() / dest.height();
        int newWidth;
        int newHeight;

        if (scaleWidthRatio < scaleHeightRatio) {
            newWidth = dest.width();//from   www.  j  a va  2 s.c o  m
            newHeight = (int) (src.height() * (1.0F / scaleWidthRatio));
            float scaleHeight = (float) newHeight / dest.height();
            if (scaleHeight < 1.0F) {
                newWidth *= scaleHeight;
                newHeight *= scaleHeight;
            }
        } else {
            newHeight = dest.height();
            newWidth = (int) (src.width() * (1.0F / scaleHeightRatio));

            float scaleWidth = (float) newWidth / dest.width();
            if (scaleWidth < 1.0F) {
                newWidth *= scaleWidth;
                newHeight *= scaleWidth;
            }
        }

        final Rect rect = new Rect(0, 0, newWidth, newHeight);
        return rect;
    }
}

Related Tutorials