Drawing a square layout inside a circle

I am trying to make a relative layout bounded within a circle i.e the relative layout should be like the square shown in the figure below.

I am trying to set width and height of the layout as:

√((diameter)²/2) which is about 70 %

public class SquareLayout extends RelativeLayout {
    public SquareLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
        int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
        int required = Math.min(originalWidth, originalHeight) * 7 / 10;

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(required, required);
    }
}

What I am getting is a rectangular layout instead of square layout:

Can anyone guide me where I am going wrong?

Sample usage:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <com.example.widget.SquareLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F55C5C">

    </com.example.widget.SquareLayout>

</RelativeLayout>

Answers


Here's how I got the solution. First I created a square frame to hold all the layouts.

public class SquareFrame extends FrameLayout {
    public SquareFrame(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
        int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
        int required = Math.min(originalWidth, originalHeight);

        super.onMeasure(
            MeasureSpec.makeMeasureSpec(required, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(required, MeasureSpec.EXACTLY));
    }
}

Then inserted all layouts within that square frame.

<com.example.widget.SquareFrame 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#5CF5FC">

    <com.example.widget.SquareLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F55C5C">

    </com.example.widget.SquareLayout>

</com.example.widget.SquareFrame>

Here is what I got a square, not a rectangle.


Need Your Help

recv function fails when used twice

c++ sockets winapi network-programming

Hello all I have been working on a Server that receives the two address from a simple client and then prints them out using Asynchronous Sockets and the WINAPI, So far I have set up the window and ...

install error with rails 4 using sudo gem install

ruby-on-rails gem ruby-on-rails-4 install

recently upgraded to os x , installed ruby 2.0 trying to install rails 4

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.