KotlinでAndroidのImageView画像を任意の角度に回転させる方法【初心者向け解説】
この記事では、Kotlinを使用してAndroidアプリのImageViewに表示した画像を、指定した角度で回転させる方法を解説します。ボタンをタップすると画像が90度回転するシンプルなサンプルを通じて、rotationプロパティの基本的な使い方を学びましょう。
完成イメージ
画面下部の「Rotate View」ボタンをタップすると、ImageViewに表示されている画像が90度回転します。実装は非常にシンプルで、imageView.rotation = 90f の1行だけで角度の指定が可能です。
手順1:新規プロジェクトを作成する
Android Studioを開き、「File」→「New Project」を選択して、必要な情報を入力し新しいプロジェクトを作成します。テンプレートは「Empty Activity」を選択しておくとスムーズです。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="600dp"
android:src="@drawable/image" />
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:text="Rotate View"
android:textStyle="bold" />
</RelativeLayout>手順3:MainActivity.kt に回転処理を実装する
src/MainActivity.kt に以下のコードを記述します。
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var imageView: ImageView
lateinit var btnRotate: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
imageView = findViewById(R.id.imageView)
btnRotate = findViewById(R.id.btnRotate)
btnRotate.setOnClickListener { imageView.rotation = 90f }
}
}ポイント:rotationプロパティとは?
Viewクラスが持つ rotation プロパティは、ビューの回転角度を度数法(degrees)で指定します。0fが基準で、値を増やすと時計回りに回転します。上記の例では90fを指定しているため、ボタンをタップするたびに画像が時計回りに90度回転します。45fや180fなど、任意の角度を設定することも可能です。
手順4:AndroidManifest.xml を確認する
androidManifest.xml は以下のようになっています。特別な権限は不要です。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>アプリを実行して動作を確認する
それでは実際にアプリを起動してみましょう。Android端末をPCに接続していることを確認してください。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。
表示されたダイアログで接続中のモバイルデバイスを選択すると、実機の画面にアプリが起動します。

初期状態では画像がそのまま表示されています。「Rotate View」ボタンをタップすると…

このように、画像が90度回転して表示されれば成功です。
まとめ
KotlinでImageViewの画像を回転させるには、rotation プロパティに角度を代入するだけなので、非常に簡単に実装できます。応用として、クリックのたびに角度を加算して連続回転させたり、ObjectAnimator を使って滑らかなアニメーション付きの回転を実装したりすることもできます。ぜひ色々な角度やアニメーションを試してみてください。
-
AndroidでImageViewの画像を指定した角度で回転させる方法
このチュートリアルでは、AndroidアプリでImageViewに表示した画像を、指定した角度で回転させる方法を解説します。サンプルとして、ボタンをタップすると画像が90度回転するシンプルなアプリを作成します。 ステップ1:新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 ステップ2:レイアウトファイル(activity_main.xml)にコードを追加する res/layout/activity_main.xml に以下のコードを記述します。
-
Androidでスムーズな画像回転アニメーションを実装する方法
このチュートリアルでは、Androidアプリで画像をスムーズに回転させるアニメーションの作成方法を、サンプルコードとともにわかりやすく解説します。回転アニメーションはXMLファイルで定義し、ボタンタップをきっかけにImageViewへ適用するシンプルな構成です。ステップ1:新規プロジェクトの作成Android Studioを起動し、「File」→「New Project」から新しいプロジェクトを作成します。必要な項目をすべて入力して、プロジェクトのセットアップを完了させてください。ステップ2:レイアウトファイル(activity_main.xml)の編集res/layout/activity_