Martin at Mennt
Martin at Mennt

Reputation: 5737

Drawing of random placed circle sometimes turn into oval

I have a small program that show a circle, and when you click on that circle it re-appears somewhere else on the screen.

This works good in 90% of the cases, but sometimes the circle is buggy. It can be that it appears outside the view, appears as an oval instead of circle, or is placed halfway outside the view.

Can anyone point me in the right direction, what am I doing wrong?

Screens:

enter image description here enter image description here enter image description here

Code example:

public class Activity1 : Activity
{
    int margin = 20;

    Button ball;
    TextView debug;
    RelativeLayout mRel;
    RelativeLayout.LayoutParams ballParams;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Create a debug label
        debug = new TextView(this);

        // Create a new ball
        ball = new Button(this);
        ball.SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.round_button));
        ball.Click += (o, e) => {
             RandomizePosition();
        };

        // Set ball parameters
        ballParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WrapContent,
        RelativeLayout.LayoutParams.WrapContent);

        // Create relative layout
        mRel = new RelativeLayout(this);
        mRel.SetBackgroundColor(Color.AntiqueWhite);
        mRel.AddView(ball);
        mRel.AddView(debug);
        SetContentView(mRel);

        // Randmize the ball position
        RandomizePosition ();
    }

    void RandomizePosition ()
    {
        // Get height and width
        Display display = WindowManager.DefaultDisplay;
        int width = display.Width;
        int height = display.Height;
        int relativeBallSize = ((((width * 2) + (height * 2)) / 100) * 3);

        // Set random parameters
        Random r = new Random();
        int maxWidth = (width - relativeBallSize);
        int maxHeight = (height - relativeBallSize);
        int x = r.Next(margin, (maxWidth < margin) ? margin : maxWidth);
        int y = r.Next(margin, (maxHeight < margin) ? margin : maxHeight);

        // Place the ball randomly
        ballParams.SetMargins(x, y, x, y);
        ball.LayoutParameters = ballParams;
        ball.SetHeight(relativeBallSize);
        ball.SetWidth(relativeBallSize);

        debug.SetText(string.Format("X = {0}, Y = {1}, Width = {2}, Height = {3}, Ball Width = {4}, Ball Height = {5}, Ball size = {6}", x, y, width, height, ball.Width, ball.Height, relativeBallSize), TextView.BufferType.Normal);
    }
}

Upvotes: 0

Views: 502

Answers (1)

Chris Hughes
Chris Hughes

Reputation: 342

Assuming that your r.Next method is working correctly I think the problem is here:

ballParams.SetMargins(x, y, x, y);

You're setting the margins for the left,top,right,bottom respectively and I don't think you mean to be setting the right and bottom margins. You might want to try using the setX and setY methods instead.

Upvotes: 2

Related Questions