【问题标题】:Android: Switch Height and Width when View change orientationAndroid:当视图改变方向时切换高度和宽度
【发布时间】:2022-01-10 06:27:42
【问题描述】:

我确实使用 PhotoView 库来放大缩小。 现在,当用户单击一个按钮时,我准备旋转 PhotoView,高度和宽度会旋转。 所以宽度小于屏幕高度。 这导致在旋转 Imageview 之前我无法像往常一样获得全屏缩放。

所以任何使旋转后的新宽度或高度成为全屏的解决方案。

示例:这是缩放图像,它不会像旋转之前那样放大整个屏幕

【问题讨论】:

  • 您能展示一下您是如何旋转图像的吗?它是库中的方法还是 View 类中的方法?

标签: android android-imageview android-orientation image-rotation android-photoview


【解决方案1】:

我必须在我的项目中使用相同类型的功能,这是我的设计和实现。

基本的XML照片视图设计

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/image_main"
        android:layout_width="0dp"
        android:scaleType="centerCrop"
        android:layout_height="400dp"
        android:src="@drawable/gull_portrait_ca_usa"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <ImageView
        android:id="@+id/image_rotate"
        android:src="@android:drawable/ic_menu_rotate"
        android:layout_margin="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_width="40dp"
        android:layout_height="40dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

然后我只更新方向onClick 事件。

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.imageRotate.setOnClickListener {
            binding.imageMain.rotation = binding.imageMain.rotation+90
        }

    }
}

Output

【讨论】:

    【解决方案2】:

    您需要创建 2 个 xml 布局。纵向(您已经在 res > layout 文件夹中创建的那个)和横向(在 res > layout-land 内)。

    当你旋转手机时,fragment 会加载 layout-land 文件夹中的 xml 文件。

    您可以在这里找到教程:https://www.geeksforgeeks.org/how-to-create-landscape-layout-in-android-studio/

    更多信息在这里:https://developer.android.com/guide/topics/resources/providing-resources

    【讨论】:

    • 我不是在旋转手机,而是使用 imageview.rotateby() 旋转图像视图本身;
    • 我点击旋转图像,这样它会旋转图像视图本身而不是手机,所以当图像旋转时,高度和宽度将被切换,高度将与旧宽度相同
    【解决方案3】:

    如果图像将被旋转,它将根据他的纵横比在屏幕上进行调整。如果你想让它适合全屏,图像会变硬,看起来不太好。如果您仍然想这样做,只需在“图像资源”的位置使用图像作为“背景”,并将视图长度和宽度保持为“匹配父级”。

    参见下面的示例视图:

    <ImageView
        android:id="@+id/mainImageID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/yourImage"/>
    

    【讨论】:

    • 我在用毕加索
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多