Get the coordinates of the center of a view

2020-04-28 07:42发布

问题:

I am trying to get the center coordinates of my custom view (a circle) so that I can draw a line from the those points. The custom view is inside a TableLayout which is itself inside a FrameLayout. However, I am having trouble - this code doesn't get it right. Any advice please?

The values given by this method are wrong on all devices. If I change the layout padding/margins etc. then the dots obviously move, but the point given by this method does not change.

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;

    }

Here is most of the code from my view class:

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

    }