Can't resize frame layout after rotating it

56 Views Asked by At

I am creating an app using Java. In it I have a frame layout that rotates on button click 90 degrees. After rotating it, I want it to resize and fit to its parent (basically the original width becomes the new height and vice versa). I have a drawing added to that layout, that's why I need for it to change its size. Right now, when I press button for the layout to rotate, that layout becomes square (both sides are equal to original width), and when I press it again, the layout once again perfectly fits inside its parent. So, I can rotate it 180 degrees without any problems, but not 90 degrees.

Right now, my code for the button in MainActivity looks like this. I tried to remove else and nothing changes, I get the same result.

spiralView2 = findViewById(R.id.spiralView); //frame layout
button = findViewById(R.id.buttonColor); //it's an image button
DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            screenWidth = displayMetrics.widthPixels;
            screenHeight = displayMetrics.heightPixels;
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    spiralView2.setRotation(90 * rotation);
                    rotation++;
                    if (rotation == 5)
                        rotation = 1;

                    RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) spiralView2.getLayoutParams();
                    if (rotation % 2 == 0) {
                        layoutParams1.height = screenWidth;
                        layoutParams1.width = screenHeight;
                    } else {
                        layoutParams1.height = screenHeight;
                        layoutParams1.width = screenWidth;
                    }
                    spiralView2.setLayoutParams(layoutParams1);
                }
            });

The drawing inside the spiralView2 is drawn in DrawGrid class, which is called by clicking button inside my fragment. When rotating some of the drawings it looks like they are redrawn after I do the rotation, so I tried to dabble width canvas params, but no luck.

The activity_main.xml part:

<RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8">

            <view
                android:id="@+id/cameraPreview"
                class="androidx.camera.view.PreviewView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/camera_preview" />

            <FrameLayout
                android:id="@+id/spiralView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <FrameLayout
                android:id="@+id/frameLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </RelativeLayout>

I can't remove weight from Relative Layout because I want for it to resize according to screen size.

Any help would be appreciated.

0

There are 0 best solutions below