Code example for RectF

Methods: centerX, centerY, height, set, width

0
     * @param r rectangle to resize 
     * @param w new width aspect 
     * @param h new height aspect 
     */ 
    public static void fixAspectRatio(RectF r, float w, float h) {
        float scale = Math.min(r.width() / w, r.height() / h);
        float centX = r.centerX();
        float centY = r.centerY();
        float hw = scale * w / 2;
        float hh = scale * h / 2;
        r.set(centX - hw, centY - hh, centX + hw, centY + hh);
    } 
 
    /** 
     * Resizes rectangle to have a certain aspect ratio (center remains 
     * stationary) while constraining it to remain within the original rect. 
     * 
     * @param r rectangle to resize 
     * @param w new width aspect 
     * @param h new height aspect 
     */