Android examples for User Interface:View Position
Get the location of a view.
/*/*from w w w . j av a2s. c o m*/ * Copyright (C) 2007 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.view.Gravity; import android.view.View; public class Main { /** * Get the location of a view. Use the gravity param to specify which part of the view to * return. * * @param v View to find * @param gravity A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, * RIGHT) * @param xy Result */ private static void getStartLocation(View v, int gravity, int[] xy) { v.getLocationOnScreen(xy); final int viewWidth = v.getWidth(); final int viewHeight = v.getHeight(); switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { case Gravity.TOP: break; case Gravity.CENTER_VERTICAL: xy[1] += viewHeight / 2; break; case Gravity.BOTTOM: xy[1] += viewHeight - 1; break; default: // Same as top -- do nothing } switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { case Gravity.LEFT: break; case Gravity.CENTER_HORIZONTAL: xy[0] += viewWidth / 2; break; case Gravity.RIGHT: xy[0] += viewWidth - 1; break; default: // Same as left -- do nothing } } }