在整个屏幕上旋转图像

发布于 2024-09-11 15:00:56 字数 878 浏览 3 评论 0原文

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

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

I can't seem to rotate properly an image on the entire screen. The problem is that when the image rotates, you can see the background in some areas. I want the image to fill the screen also when it rotates. Here is the layout. I've tried different scale types without success.

<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>

And here is the code that I am using.

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

I start the animation in onResume. As I said, the image rotates but there are background areas when it does rotate. I've tried using images much bigger than the screen but still it doesn't do what I want. I don't care that outer regions of the image are not visible. I just want the rotation to fill the screen. I guess ImageView sets its width and height initially and then, when it rotates, it uses those dimensions. Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-09-18 15:00:56

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

尝试将scaleType更改为“center”。

You are setting the scaleType of your ImageView to "centerCrop". Unfortunatly there isn't good documentation on the exact meaning of the different scaleTypes, but the name implies that the image is centered on the screen and cropped to the exact size of the ImageView. Thus, when you begin to rotate you will see background areas.

Try chaging the scaleType to just "center".

海拔太高太耀眼 2024-09-18 15:00:56

经过更多地研究问题后,我认为不可能实现我想要的目标。如果可以的话,那就太复杂了,不值得。

After looking more at the problem, I don't think it's possible to achieve what I want. If it is possible, then it is too complicated and it's not worth it.

长梦不多时 2024-09-18 15:00:56

你可以 :

<?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
    }

You can :

<?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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文