android studio rotate imageview
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.widget.ImageView
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
private var currentRotation: Float = 0.0f
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageView = findViewById(R.id.imageView)
imageView.setOnClickListener {
rotateImage()
}
}
private fun rotateImage() {
currentRotation += 90.0f
if (currentRotation == 360.0f) {
currentRotation = 0.0f
}
imageView.animate().rotation(currentRotation).start()
}
}
Make sure to add the following code in your res/layout/activity_main.xml
file:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image_resource"
android:layout_centerInParent="true"/>
Replace "your_image_resource"
with the actual resource ID of your image.