is Point Inside View - Android User Interface

Android examples for User Interface:View Contains

Description

is Point Inside View

Demo Code


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

import android.view.View;

public class Main {
    public static boolean isPointInsideView(float x, float y, View view) {
        RectF bounds = getViewBounds(view);
        return isPointInsideRect(x, y, bounds);
    }//from w  w  w.  ja v a  2  s. c  o m

    public static RectF getViewBounds(View view) {
        int location[] = new int[2];
        view.getLocationOnScreen(location);
        int viewX = location[0];
        int viewY = location[1];

        return new RectF(viewX, viewY, viewX + view.getWidth(), viewY
                + view.getHeight());
    }

    public static boolean isPointInsideRect(float x, float y, RectF rect) {
        return x > rect.left && x < rect.right && y > rect.top
                && y < rect.bottom;
    }
}

Related Tutorials