Lays out a view so that its center will be on cx and cy - Android User Interface

Android examples for User Interface:View Center

Description

Lays out a view so that its center will be on cx and cy

Demo Code


//package com.java2s;
import android.view.View;

public class Main {
    /**//from  w w  w . ja v a  2  s .c o  m
     * Lays out a view so that its center will be on {@code cx} and {@code cy}
     *
     * @param view The view to layout
     * @param cx   The X coordinate of the location in the parent in which to place the view
     * @param cy   The Y coordinate of the location in the parent in which to place the view
     */
    static void layoutFromCenter(View view, int cx, int cy) {
        int left = cx - view.getMeasuredWidth() / 2;
        int top = cy - view.getMeasuredHeight() / 2;
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        view.layout(left, top, right, bottom);
    }
}

Related Tutorials