get Center between two Rectangle - Android Graphics

Android examples for Graphics:Rectangle

Description

get Center between two Rectangle

Demo Code


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

public class Main {
    public static final int CENTER = 0;
    public static final int CENTER_VERTICAL = 2;
    public static final int CENTER_HORIZONTAL = 1;

    public static Rect getCenter(Rect outer, Rect inner, int centerType) {
        int w = inner.width();
        int h = inner.height();
        switch (centerType) {
        case CENTER:
            inner.top = (outer.height() - h) / 2 + outer.top;
            inner.bottom = inner.top + h;
        case CENTER_HORIZONTAL:
            inner.left = (outer.width() - w) / 2 + outer.left;
            inner.right = inner.left + w;
            break;
        case CENTER_VERTICAL:
            inner.top = (outer.height() - h) / 2 + outer.top;
            inner.bottom = inner.top + h;
            break;
        }/*from ww  w.java 2s  . c  om*/
        return inner;
    }
}

Related Tutorials