map points inside view into locations of a screen - Android User Interface

Android examples for User Interface:View

Description

map points inside view into locations of a screen

Demo Code

/*/*from  w ww.  jav a 2 s  .c  om*/
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.graphics.Matrix;
import android.graphics.RectF;

import android.view.View;
import android.view.ViewParent;

public class Main {
    private static final float[] sTmpFloat4 = new float[4];

    /**
     * map points inside view into locations of a screen
     * The function is an extension of {@link View#getLocationInWindow } and can map
     * multiple points.
     *
     * @param points x[0], y[0], x[1], y[1], ...
     */
    public static void getLocationsInWindow(View view, float[] points) {

        if (points == null || (points.length & 1) != 0) {
            throw new IllegalArgumentException();
        }
        int length = points.length;
        Matrix matrix = view.getMatrix();
        if (matrix != null && !matrix.isIdentity()) {
            matrix.mapPoints(points);
        }

        int deltax = view.getLeft();
        int deltay = view.getTop();
        for (int i = 0; i < length;) {
            points[i] += deltax;
            i++;
            points[i] += deltay;
            i++;
        }

        ViewParent viewParent = view.getParent();
        while (viewParent instanceof View) {
            view = (View) viewParent;

            deltax = view.getScrollX();
            deltay = view.getScrollY();
            for (int i = 0; i < length;) {
                points[i] -= deltax;
                i++;
                points[i] -= deltay;
                i++;
            }

            matrix = view.getMatrix();
            if (matrix != null && !matrix.isIdentity()) {
                matrix.mapPoints(points);
            }

            deltax = view.getLeft();
            deltay = view.getTop();
            for (int i = 0; i < length;) {
                points[i] += deltax;
                i++;
                points[i] += deltay;
                i++;
            }

            viewParent = view.getParent();
        }
    }

    /**
     * get locations of view bounds in Window
     */
    public static void getLocationsInWindow(View view, RectF rect) {
        sTmpFloat4[0] = rect.left;
        sTmpFloat4[1] = rect.top;
        sTmpFloat4[2] = rect.right;
        sTmpFloat4[3] = rect.bottom;
        getLocationsInWindow(view, sTmpFloat4);
        rect.left = sTmpFloat4[0];
        rect.top = sTmpFloat4[1];
        rect.right = sTmpFloat4[2];
        rect.bottom = sTmpFloat4[3];
    }
}

Related Tutorials