KotlinでAndroidビューの背景色の変化をアニメーション化する方法
この記事では、Kotlinを使用してAndroidアプリのビュー(View)の背景色の変化をアニメーションで表示する方法を解説します。TransitionDrawableを活用することで、複数の色を滑らかにクロスフェードさせることができます。
ステップ1:新規プロジェクトの作成
Android Studioを開き、「File」→「New Project」を選択して、必要な項目を入力し、新しいプロジェクトを作成します。
ステップ2:レイアウトファイル(res/layout/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:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/textView" android:layout_centerInParent="true" android:layout_marginBottom="15dp" android:text="Animate background color" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Changing Background color of this view." android:textAlignment="center" android:textColor="@android:color/background_dark" android:textSize="36sp" android:textStyle="bold" /> </RelativeLayout>
ステップ3:MainActivity.ktの編集
以下のコードをsrc/MainActivity.ktに追加します。
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.TransitionDrawable
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button.setOnClickListener {
val colorDrawables = arrayOf(ColorDrawable(Color.GREEN),
ColorDrawable(Color.RED), ColorDrawable(Color.YELLOW))
val transitionDrawable = TransitionDrawable(colorDrawables)
textView.background = transitionDrawable
transitionDrawable.startTransition(2000)
}
}
}
このコードのポイントは、ColorDrawableで複数の色(緑・赤・黄)を定義し、それらをTransitionDrawableに渡している点です。startTransition(2000)を呼び出すことで、2秒かけて色が順番に変化するアニメーションが実行されます。
ステップ4:AndroidManifest.xmlの編集
以下のコードをandroidManifest.xmlに追加します。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.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端末がパソコンに接続されていることを前提とします。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの実行アイコン
をクリックします。表示された選択肢から接続したモバイルデバイスを選択すると、端末にデフォルトの画面が表示されます。

-
ActionBarActivityでActionBarの背景色を変更する方法
この記事では、ActionBarActivityのActionBar(アクションバー)の背景色を変更する手順について解説します。テーマのスタイル定義を編集することで、アプリ全体のデザインに合わせたカスタマイズが可能になります。ステップ1:新規プロジェクトの作成Android Studioを起動し、「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な項目をすべて入力してプロジェクトのセットアップを完了させてください。ステップ2:レイアウトファイルの編集res/layout/activity_main.xmlに以下のコードを追加します。<?xml vers
-
Androidのオプションメニューの背景色を変更する方法を徹底解説
Androidのオプションメニューの背景色を変更する方法この記事では、Androidアプリのオプションメニュー(オプションズメニュー)の背景色を変更する方法を、実際に動作するサンプルコードとともにステップごとに解説します。テーマ(styles.xml)にandroid:itemBackgroundを設定するだけで、メニューの背景色を簡単にカスタマイズできます。ステップ1:新規プロジェクトの作成まず、Android Studioで新しいプロジェクトを作成します。メニューからFile → New Projectを選択し、必要な項目をすべて入力してプロジェクトを作成しましょう。ステップ2:レイアウト