【问题标题】:when trying to rotate layout image goes out of screen尝试旋转布局图像时离开屏幕
【发布时间】:2019-12-08 17:17:15
【问题描述】:

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

  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>

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

之前

之后(按下旋转按钮)

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

【问题讨论】:

  • 可以发截图吗?
  • 已添加请查看
  • @JDevoloper 你能在图片旋转之前发布图片吗?
  • 已发布,请查看@VarunRaj
  • 在您的情况下,旋转点应该是height/2width/2 的视图中心。

标签: android image layout rotation


【解决方案1】:

设置轴

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

【讨论】:

  • 我已经添加了它们。 “0”不是表示屏幕开始的意思吗? transformpivotX 和 Y 的值应该是多少?
  • 将 X 的平移设置为图像宽度的一半,将 Y 的平移设置为高度的一半。在旋转之前执行此操作。
  • 在 X 和 Y 旋转之前记录它的平移,然后您可以确定它的设置正确“linearLayout.getTranslationX()”
  • 布局前后,getTranslationX 和 Y 为 0.0
  • 尝试使用 pivot 代替
【解决方案2】:

对于视图的旋转使用如下-

在下方旋转 180 度使用

   layout.animate().rotation(180f).setDuration(100).start()

在下方转回正常使用

   layout.animate().rotation(0f).setDuration(100).start()

【讨论】:

  • 我的问题不是轮换。旋转布局后它会离开屏幕!
  • 为什么要使用 setTranslationX... setTranslationX 用于将视图从一个位置转换到另一个位置.. 我认为您必须删除 setTranslationX 和 setTranslationY
  • 请发送您的完整 xml 文件
  • 我认为问题在于图像是否全屏。当我加载全屏图像时,它工作正常。当我尝试加载较小的图像时,如您在我的帖子中看到的那样,它不起作用。可能这适用于全屏图像。
  • 如果你想旋转图像而不是你为什么要旋转完整的布局?
【解决方案3】:

当您旋转视图/视图组时,它会随主状态旋转 (land/PORTRAIT) 宽度为 ex - 你在 PORTRAIT 并且宽度是 '250' 并且高度是“150”并且您将视图旋转设置为着陆它不 更新到新的宽度和高度,因此你弄错了 行为,为了解决您的问题,我创建了简单的演示

XML

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
    <androidx.constraintlayout.widget.ConstraintLayout
            android:orientation="vertical"
            android:background="@android:color/holo_red_light"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout">
        <ImageView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                android:src="@drawable/ic_launcher_foreground"
                tools:srcCompat="@tools:sample/avatars[1]"
                android:id="@+id/imageView" app:layout_constraintTop_toBottomOf="@+id/textView"
                android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent" android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"/>
        <TextView
                android:text="Title"
                android:background="@android:color/holo_red_dark"
                android:layout_width="150dp"
                android:textSize="20sp"
                android:textColor="@color/colorPrimary"
                android:layout_height="wrap_content" android:id="@+id/textView"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginTop="16dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp"
                app:layout_constraintVertical_bias="0.0"/>
        <Button
                android:text="Rotate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                android:id="@+id/button" android:layout_marginTop="8dp"
                app:layout_constraintTop_toTopOf="@+id/imageView" android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

活动

import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import de.conlance.kotlinclean.R
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.ActivityInfo.*
import android.content.res.Configuration.ORIENTATION_LANDSCAPE
import android.os.Build.VERSION.SDK_INT
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.stackoverflow_lo.*

class MainActivitySOF : AppCompatActivity() {

    var is90Degre = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.stackoverflow_lo)
        button.setOnClickListener {
            rotate()
        }
    }


    fun rotate() = if (getResources().getConfiguration().orientation.equals(ORIENTATION_LANDSCAPE))
    {
        Toast.makeText(this@MainActivitySOF, "Portrait", Toast.LENGTH_SHORT).show()
        setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT)
        Thread.sleep(50)




    } else {
        Toast.makeText(this@MainActivitySOF, "LandScape", Toast.LENGTH_SHORT).show()
        setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE)
        Thread.sleep(50)



    }

    fun launchService() {
        if (SDK_INT > 25) {
            Log.v("MainActivitySOF", "25----------------------------")
            this@MainActivitySOF.startForegroundService(Intent(this@MainActivitySOF, ForGroundService::class.java))
        } else {
            startService(Intent(this, ForGroundService::class.java))
        }
    }

}

【讨论】:

  • 感谢您的回答。但你可能误解了我的问题。我不想旋转屏幕。我想通过给出度数来旋转布局或视图。横向或纵向模式不是我的答案。我已经解决了根据不断变化的图像调整布局大小的问题。
【解决方案4】:

添加您的 LinearLayout 并设置 match_parent 布局的宽度和高度

android:layout_gravity="center"


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2017-10-23
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多