在整个屏幕上旋转图像

时间:2010-07-28 15:53:20

标签: android animation rotation

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

<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最初设置它的宽度和高度然后,当它旋转时,它使用那些尺寸。有更好的方法吗?

3 个答案:

答案 0 :(得分:0)

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

尝试将scaleType转换为“居中”。

答案 1 :(得分:0)

在仔细研究这个问题后,我认为不可能实现我想要的目标。如果有可能,那就太复杂了,不值得。

答案 2 :(得分:0)

您可以:

<?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
    }
相关问题