In this tutorial, you will learn how to develop an Android application using Kotlin that enables users to rotate an image using left and right buttons. The tutorial will guide you through implementing logic to adjust the rotation angle of an ImageView each time a button is clicked. You'll explore how to handle user interactions with buttons to increment or decrement the rotation angle, update the ImageView dynamically, and ensure smooth visual feedback as the image rotates. By following this step-by-step guide, you'll gain practical experience in managing image transformations, handling button clicks, and enhancing user interaction through rotational functionality in your Kotlin-based Android applications.
Submitted on July 07, 2024
To create an Android app that allows rotating an image using left and right buttons, you'll need to implement logic to rotate the image by a certain degree each time a button is clicked. Here's a step-by-step guide to achieve this using Kotlin:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/sample_image"
android:scaleType="center"
android:adjustViewBounds="true"
android:layout_above="@id/buttons_layout"/>
<LinearLayout
android:id="@+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:padding="16dp">
<Button
android:id="@+id/btnRotateLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Left"
android:onClick="rotateLeft"/>
<Button
android:id="@+id/btnRotateRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Right"
android:onClick="rotateRight"/>
</LinearLayout>
</RelativeLayout>
package com.example.rotateimage
import android.graphics.Bitmap
import android.graphics.Matrix
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var imageView: ImageView
private lateinit var bitmap: Bitmap
private var degree = 0
@Override
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
imageView = findViewById(R.id.imageView)
bitmap = (imageView.drawable).toBitmap() // Convert ImageView drawable to Bitmap
}
fun rotateLeft(view: android.view.View) {
degree -= 90
rotateImage(degree)
}
fun rotateRight(view: android.view.View) {
degree += 90
rotateImage(degree)
}
private fun rotateImage(degrees: Int) {
val matrix = Matrix()
matrix.postRotate(degrees.toFloat())
val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
imageView.setImageBitmap(rotatedBitmap)
}
}
This implementation allows users to interactively rotate an image left and right using buttons in an Android app using Kotlin.