How to rotate a view in android and also adjust its height in respect to parent view

Issue

I am trying to rotate a view to any desired position from 0 to 360, It is a floating view and it is supposed to also go to the edges of the screen so I don’t want to create a square box to rotate the view I want it just to take the place it needed in order to view the rotated view.

I am able to rotate the view but the issue is it is getting cropped by the parent and only the initial view is visible and the rest of it is being cropped. As shown in the image below:

enter image description here

Here is the code I am using:

public void rotateView(int angle) {
        floatingView.setRotation(angle);
    }

One Solution is I am thinking of using a diagonal value of the view as I rotate the view to set new height and width but not sure how to use it to just consider the width or height for the rotated part.

Solution

you can check this library it makes it easy for you: https://github.com/rongi/rotate-layout\
because it has the feature you looking for, The bounding box is rotated with the views inside.

Example

Usage

implementation 'rongi.rotate-layout:rotate-layout:3.0.0'
<com.github.rongi.rotate_layout.layout.RotateLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:angle="90"> <!-- Specify rotate angle here -->

    <YourLayoutHere
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </YourLayoutHere>
</com.github.rongi.rotate_layout.layout.RotateLayout>

Answered By – Aiman Alyosofi

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *