get View Coordinates - Android User Interface

Android examples for User Interface:View

Description

get View Coordinates

Demo Code

/*//from ww w. ja  v  a 2  s  . c  om
 * Copyright 2014 Randomly Typing LLC
 *
 * 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.Point;
import android.graphics.Rect;

public class Main {
    public static void getViewCoordinates(Point coordinates,
            int displayOrientation, boolean frontFacing,
            Rect viewCoordinatesRange) {
        float[] coordinateArray = { coordinates.x, coordinates.y };
        getViewCoordinates(coordinateArray, displayOrientation,
                frontFacing, viewCoordinatesRange);
        coordinates.x = Math.round(coordinateArray[0]);
        coordinates.y = Math.round(coordinateArray[1]);
    }

    public static void getViewCoordinates(float[] coordinates,
            int displayOrientation, boolean frontFacing,
            Rect viewCoordinatesRange) {
        final Matrix matrix = new Matrix();
        matrix.setScale(frontFacing ? -1 : 1, 1);
        matrix.postRotate(displayOrientation);
        final int width = viewCoordinatesRange.width();
        final int height = viewCoordinatesRange.height();
        matrix.postScale(width / 2000f, height / 2000f);
        matrix.postTranslate(width * 0.5f, height * 0.5f);
        matrix.mapPoints(coordinates);
    }
}

Related Tutorials