1

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

    }
  • make sure you are doing your calculations in `onMeasure` otherwise `getWidth()` might return 0 and your code won't run properly. – Ankit Popli Oct 27 '13 at 07:42
  • float yCenter = yLoc - (getWidth()/2); ? Is it squared? – Blackbelt Oct 27 '13 at 07:47
  • @blackbelt It's a circle - sorry, I should have mentioned that. –  Oct 27 '13 at 07:54
  • @AnkitPopli No, it is not in `onMeasure`, but I am not calling the method until the view is touched. –  Oct 27 '13 at 07:55
  • it would be helpful if you can share some more code, because this seems fine. – Ankit Popli Oct 27 '13 at 08:02
  • @AnkitPopli I have added most of the code for the view. –  Oct 27 '13 at 08:05
  • 1
    sorry but what is wrong with (getTop() + getWidth()/2); and getLeft()+getWidth()/2); ? – Blackbelt Oct 27 '13 at 08:17
  • @blackbelt That gives me the coordinates within the view itself, but not in relation to all the other views - and that is what I'm trying to get. Please correct me if I'm wrong! –  Oct 27 '13 at 08:20
  • 1
    it returns the view position in parent coords – Blackbelt Oct 27 '13 at 08:23
  • @blackbelt So if a have a `TableLayout` and I want to draw a line between two views within this layout, I can use `(getTop() + getWidth()/2); and getLeft()+getWidth()/2);` to get the correct coordinates for the center of each view? –  Oct 27 '13 at 08:30
  • I just tried @blackbelt 's code. This works for x values, but not for y values. The y's are not changing. –  Oct 27 '13 at 08:38
  • are you it is a circle? have you tried using getHeight instead of getWidth? – Blackbelt Oct 27 '13 at 08:45
  • @blackbelt http://stackoverflow.com/q/19604968/2442638 In this question there are screenshots showing the dots. `getHeight() didn't make a difference. –  Oct 27 '13 at 08:50
  • It seems strange to me. Still you can try yo calculate the height of that bar and to apply some sort of padding – Blackbelt Oct 27 '13 at 08:58
  • @blackbelt But the Y value is not changing at all, so I don't think adding in the padding would make a difference. –  Oct 27 '13 at 09:00
  • @blackbelt `getTop()` is returns `2` all the time. –  Oct 27 '13 at 09:23
  • It could be correct. getTop returns the top coords of your view in parent coordinates.. I dont know why it should work for the X coord and not for the y – Blackbelt Oct 27 '13 at 09:32
  • @blackbelt Thanks for helping. I asked another question, to see if there is a solution to that specific problem. –  Oct 27 '13 at 09:35

0 Answers0