首页 > 解决方案 > 尝试旋转布局图像时离开屏幕

问题描述

我正在尝试旋转包含图像的布局。按下时,按钮布局正在旋转,但它会离开屏幕。我试过这样旋转;

  private void changeRotation(float rotation) {
    layout.setRotation(rotation);
    layout.setTranslationX(0);
    layout.setTranslationY(0);
    layout.requestLayout();
    isRotated = true;
    newRotationValue = rotation;
}

我的布局是这样的;

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/transparent"
    tools:context=".activity.CropActivity">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="8dp"
        android:orientation="horizontal"
        android:weightSum="3">

        <info.hoang8f.widget.FButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:drawableStart="@drawable/ic_camera"
            android:onClick="cameraBtnClick"
            android:padding="16dp"
            android:text="Camera"
            android:textColor="@color/colorPrimaryDark"
            app:cornerRadius="16dp"
            app:fButtonColor="@color/colorAccent"
            app:shadowColor="@color/colorBlack"
            app:shadowEnabled="true"
            app:shadowHeight="2dp" />


        <info.hoang8f.widget.FButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:drawableStart="@drawable/ic_gallery"
            android:onClick="galleryBtnClick"
            android:padding="16dp"
            android:text="Gallery"
            android:textColor="@color/colorPrimaryDark"
            app:cornerRadius="16dp"
            app:fButtonColor="@color/colorAccent"
            app:shadowColor="@color/colorBlack"
            app:shadowEnabled="true"
            app:shadowHeight="2dp" />

        <info.hoang8f.widget.FButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:drawableStart="@drawable/ic_rotate"
            android:onClick="rotateBtnClick"
            android:padding="16dp"
            android:text="Rotate"
            android:textColor="@color/colorPrimaryDark"
            app:cornerRadius="16dp"
            app:fButtonColor="@color/colorAccent"
            app:shadowColor="@color/colorBlack"
            app:shadowEnabled="true"
            app:shadowHeight="2dp" />

    </LinearLayout>

</FrameLayout>

如果图像是全屏的,则没有问题。但图像是这样的,出了点问题。

ss

之后(按旋转按钮)ss

如何解决这个问题呢 ?请帮助我,谢谢

标签: androidimagelayoutrotation

解决方案


设置枢轴

layout.setPivotX(layout.getWidth() / 2);
layout.setPivotY(layout.getHeight() / 2);

推荐阅读