Obtén las coordenadas del centro de una vista.

Estoy tratando de obtener las coordenadas del centro de mi vista personalizada (un círculo) para poder dibujar una línea desde esos puntos. La vista personalizada está dentro de unTableLayout que está dentro de unFrameLayout. Sin embargo, estoy teniendo problemas, este código no lo hace bien. ¿Algún consejo por favor?

Los valores dados por este método son incorrectos en todos los dispositivos. Si cambio el relleno / márgenes de diseño, etc., los puntos obviamente se mueven, pero el punto dado por este método no cambia.

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;

    }

Aquí está la mayoría del código de mi clase de vista:

        // 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;
}

    }

Respuestas a la pregunta(0)

Su respuesta a la pregunta