Rotate an image on the entire screen
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?
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".
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.
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
}
精彩评论