我如何计算到小部​​件的距离(顶部,中部和底部)?

时间:2018-04-11 15:23:54

标签: android distance anchorpoint

我正在我的应用程序底部开发一个幻灯片,它有一个设置锚点的选项,所以我想让幻灯片每次都保持在某个标签下面,所以我需要以百分比计算点数(从0.0到1.0)小部件是什么。

Screenshot

我如何计算这些距离?红色,紫色和蓝色线?

1 个答案:

答案 0 :(得分:0)

我已经在一个普通的工具中开发了下一个方法。

public static Point getLocationOnScreen(View view){
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    return new Point(location[0], location[1]);
}

public static Point getLocationInWindow(View view){
    int[] location = new int[2];
    view.getLocationInWindow(location);
    return new Point(location[0], location[1]);
}

/**
 * Gets the device's native screen resolution rotated
 * based on the device's current screen orientation.
 */
@SuppressLint("ObsoleteSdkInt")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) //To remove warning about using
//getRealSize prior to API 17
public static Point screenResolution(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point screenResolution = new Point();

    if (Build.VERSION.SDK_INT < 14)
        throw new RuntimeException("Unsupported Android version.");
    display.getRealSize(screenResolution);

    return screenResolution;
}