get View Center Point - Android User Interface

Android examples for User Interface:View Center

Description

get View Center Point

Demo Code


//package com.java2s;

import android.graphics.Point;
import android.support.annotation.NonNull;
import android.view.View;

public class Main {
    public static Point getViewCenterPoint(@NonNull View view) {
        Point center = getViewLocation(view);
        center.x += view.getWidth() / 2;
        center.y += view.getHeight() / 2;

        return center;
    }// ww w .  j  av a  2s. c  om

    public static Point getViewLocation(@NonNull View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int centerX = location[0];
        int centerY = location[1];

        return new Point(centerX, centerY);
    }
}

Related Tutorials