【问题标题】:Rotate an image on the entire screen在整个屏幕上旋转图像
【发布时间】:2011-03-22 06:01:44
【问题描述】:

我似乎无法在整个屏幕上正确旋转图像。问题是当图像旋转时,您可以在某些区域看到背景。我希望图像在旋转时也能填满屏幕。这是布局。我尝试了不同的比例类型,但没有成功。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop" />
</LinearLayout>

这是我正在使用的代码。

image = (ImageView) findViewById(R.id.image);
image.setBackgroundResource(R.drawable.back_big);
rot = AnimationUtils.loadAnimation(this, R.anim.rotation);
image.startAnimation(rot);

我在onResume 中开始动画。正如我所说,图像会旋转,但旋转时会有背景区域。我尝试过使用比屏幕大得多的图像,但它仍然没有达到我想要的效果。我不在乎图像的外部区域不可见。我只是想让旋转填满屏幕。我猜ImageView 最初设置它的宽度和高度,然后,当它旋转时,它使用这些尺寸。有没有更好的方法来做到这一点?

【问题讨论】:

    标签: android animation rotation


    【解决方案1】:

    您将 ImageView 的 scaleType 设置为“centerCrop”。不幸的是,没有关于不同 scaleTypes 确切含义的良好文档,但名称暗示图像位于屏幕中心并裁剪为 ImageView 的确切大小。因此,当您开始旋转时,您会看到背景区域。

    尝试将 scaleType 更改为“center”。

    【讨论】:

    • 我已经尝试了所有的 scaleTypes 但没有成功。我认为 ImageView 将图像的初始宽度和高度设置为屏幕的宽度和高度,然后,当它旋转时,它不会动态地改变图像大小。所以我认为 scaleType 并不重要。
    【解决方案2】:

    在更多地查看问题后,我认为不可能实现我想要的。如果可以,那就太复杂了,不值得。

    【讨论】:

      【解决方案3】:

      你可以:

      <?xml version="1.0" encoding="utf-8"?>
      <layout>
      
          <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">
      
              <ImageView
                  android:id="@+id/image"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:src="@drawable/pic"
                  app:layout_constraintBottom_toBottomOf="parent"
                  app:layout_constraintLeft_toLeftOf="parent"
                  app:layout_constraintRight_toRightOf="parent"
                  app:layout_constraintTop_toTopOf="parent" />
          </androidx.constraintlayout.widget.ConstraintLayout>
      </layout>
      
        private var sfRotation = 0
          private lateinit var binding: ActivityMainBinding
          private var scale = 1F
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
              val outMetrics = DisplayMetrics()
              windowManager.getDefaultDisplay().getRealMetrics(outMetrics)
              val w = outMetrics.widthPixels
              val h = outMetrics.heightPixels
              scale = if (w > h) w.toFloat() / h.toFloat() else h.toFloat() / w.toFloat()
              binding.image.scaleX = scale
              binding.image.scaleY = scale
          }
      
          fun set() {
              sfRotation = (sfRotation + 90) % 360
              Log.d(">>>sfRotation", sfRotation.toString())
              Log.d(">>>hwrotation", hwRotation.toString())
              binding.image.rotation = sfRotation.toFloat()
              binding.image.scaleX = scale
              binding.image.scaleY = scale
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        相关资源
        最近更新 更多