Android
 Computer >> コンピューター >  >> プログラミング >> Android

【Android】TransitionDrawableを使ってViewの背景色の変化をアニメーション化する方法

はじめに

このチュートリアルでは、AndroidアプリでViewの背景色の変化をアニメーションとして表示する方法を解説します。

鍵となるのはTransitionDrawableクラスです。複数のDrawableを重ねて管理し、指定した時間をかけて滑らかにクロスフェード(色の遷移)を行える便利な仕組みです。今回はボタンをタップすると、TextViewの背景が「赤 → 青 → 緑」と徐々に変化するサンプルを作成していきます。

手順1:新規プロジェクトの作成

まず、Android Studioで新しいプロジェクトを作成しましょう。メニューから「File」→「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成します。

手順2:レイアウトファイル(activity_main.xml)の編集

res/layout/activity_main.xml に以下のコードを記述します。画面中央にTextViewを配置し、その上に背景色を切り替えるトリガーとなるButtonを設置しています。

<?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:id="@+id/relativeLayout"
    android:padding="8dp"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:text="Animate background color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView"
        android:layout_centerInParent="true"
        android:layout_marginBottom="15dp"/>
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="36sp"
        android:textStyle="bold"
        android:text="Changing Background color of this view."
        android:layout_centerInParent="true" />
</RelativeLayout>

手順3:MainActivity.java の実装

続いて、src/MainActivity.java に以下のコードを追加します。

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorDrawable[] colorDrawables = {new ColorDrawable(Color.RED),
                new ColorDrawable(Color.BLUE), new ColorDrawable(Color.GREEN)};
                TransitionDrawable transitionDrawable = new TransitionDrawable(colorDrawables);
                textView.setBackground(transitionDrawable);
                transitionDrawable.startTransition(2000);
            }
        });
    }
}

コードの解説

ボタンがクリックされると、Color.REDColor.BLUEColor.GREENの3つのColorDrawableを配列として用意し、それをもとにTransitionDrawableを生成します。生成したTransitionDrawableをTextViewの背景にセットし、startTransition(2000)を呼び出すことで、約2秒(2000ミリ秒)かけて色が順番に遷移していきます。

手順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.sample">
    <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」アイコンをクリックします。デバイス選択ダイアログで接続中のモバイル端末を選択すると、端末側に以下のような初期画面が表示されます。

【Android】TransitionDrawableを使ってViewの背景色の変化をアニメーション化する方法


【Android】TransitionDrawableを使ってViewの背景色の変化をアニメーション化する方法


  1. Outlookカレンダーの背景色を変更する方法|個別・一括変更の手順を解説

    Outlookカレンダーは、メールや連絡先などの機能と統合されたスケジュール管理ツールです。現在のカレンダーの背景色が気に入らない場合は、いつでも好きな色に変更できます。特に、Outlookで複数のカレンダーを使い分けている場合には、カレンダーごとに色を変えることで見分けやすくなり、とても便利です。 Outlookカレンダーの背景色を変更する手順 Outlookでカレンダーの背景色を変更するには、以下の手順に従ってください。 Outlookを起動する 「予定表」ボタンをクリックする カレンダー上の任意の場所を右クリックする コンテキストメニューの「色」にカーソルを合わせる 好きな色を選択する

  2. 【iOS】メモアプリの背景色を変更する方法|ライト/ダーク切り替えと罫線・グリッド設定も解説

    iOSの標準「メモ」アプリを使っていると、背景が端末全体の表示設定に自動的に連動してしまう点に不便さを感じることがあります。ライトモードが有効なときは明るい背景に、ダークモードのときは暗い背景になり、個別にコントロールできないように見えます。しかし実は、iOSではメモごとの背景色を簡単に変更できます。この記事では、特定のメモだけ背景を変える方法、すべてのメモに一括で適用する方法、さらに罫線やグリッド背景を追加するカスタマイズ術まで、わかりやすく解説します。注意:メモアプリで異なる背景を設定するには、端末がiOS 13 / iPadOS 13以降である必要があります。古いバージョンをお使いの場合