Получить координаты центра вида

Я пытаюсь получить координаты центра моего пользовательского вида (круг), чтобы я мог нарисовать линию из этих точек. Пользовательский вид находится внутриTableLayout который сам внутриFrameLayout, Тем не менее, у меня возникли проблемы - этот код не понимает это правильно. Любой совет, пожалуйста?

Значения, заданные этим методом, неверны на всех устройствах. Если я изменю отступы / поля макета и т. Д., То точки, очевидно, сместятся, но точка, заданная этим методом, не изменится.

public float[] getDotCenterLocationOnScreen() {

        int[] location = new int[2];
        getLocationOnScreen(location);

        int xLoc = location[0];
        int yLoc = location[1];

        float xCenter = xLoc + (getWidth()/2);
        float yCenter = yLoc - (getWidth()/2);

        float[] dotCenterLocation =  { xCenter, yCenter };

        return dotCenterLocation;

    }

Вот большая часть кода из моего класса просмотра:

        // Radius of Dot
        int RADIUS;
        private static final int START_RADIUS = 30;

        // Value of which the Radius is multiplied by to get full width & height of
        // the DotView
        int sizeMultiplier = 4;

        // Define other Objects
        private Paint paint = new Paint();      

        float mTranslateX;
        float mTranslateY;

        public DotView(Context context, AttributeSet attrs) {
            super(context, attrs);

            paint.setAntiAlias(true);
            paint.setStrokeWidth(6f);
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setColor(Color.BLACK);

            RADIUS = START_RADIUS;

        }

        public void setRadius(int radius) {
            RADIUS = radius;
            invalidate();
        }

        public int getRadius() {
            return RADIUS;
        }

        public void onDraw(Canvas canvas) {

            super.onDraw(canvas);
            canvas.save();
            canvas.translate(mTranslateX, mTranslateY);
            canvas.drawCircle(0, 0, RADIUS, paint);
            canvas.restore();
        }

        public float[] getDotCenterLocationOnScreen() {

            int[] location = new int[2];
            getLocationOnScreen(location);

            int xLoc = location[0];
            int yLoc = location[1];

            float xCenter = xLoc + (getWidth()/2);
            float yCenter = yLoc - (getWidth()/2);

            float[] dotCenterLocation =  { xCenter, yCenter };

            return dotCenterLocation;

        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            final int dia = START_RADIUS * sizeMultiplier; // Multiplying by 2 makes
                                                            // boundaries exactly the same size a dot.

            int w = resolveSize(dia, widthMeasureSpec);
            int h = resolveSize(dia, heightMeasureSpec);
            setMeasuredDimension(w, h);
            float radius = Math.min(w, h) / 2F;
            mTranslateX = radius;
            mTranslateY = radius;
}

    }

Ответы на вопрос(0)

Ваш ответ на вопрос