XML値ファイルを使って配列内にR.drawable.*形式のドローアブルリソースIDを保存する方法【Android開発】
はじめに
この記事では、Androidアプリ開発において、XML値ファイル(valuesファイル)を使用して、配列の中に R.drawable.* 形式のドローアブルリソースIDを保存する方法を解説します。
通常、ドローアブルリソースIDはコード内で直接指定することが多いですが、XMLの配列リソースとして定義しておけば、画像の管理がしやすくなり、コードの保守性も向上します。ここでは TypedArray を使って、XMLで定義した画像リソースを取得して表示する実装例を紹介します。
手順1:新規プロジェクトを作成する
Android Studioで新しいプロジェクトを作成します。メニューから「File」⇒「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。
手順2:arrays.xmlに配列リソースを定義する
次のコードを res/values/arrays.xml に追加します。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="random_images">
<item>@drawable/image1</item>
<item>@drawable/image2</item>
<item>@drawable/image3</item>
</integer-array>
</resources>ポイントは <integer-array> タグを使用することです。@drawable/xxx の形式で記述すると、各アイテムがドローアブルリソースへの参照として登録され、Javaコード側からリソースIDとして取得できるようになります。
手順3:レイアウトファイルを作成する
次のコードを res/layout/activity_main.xml に追加します。ここでは、画像を表示するためのImageViewを3つ縦に並べています。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:contentDescription="@string/app_name" />
<ImageView
android:id="@+id/iv2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:contentDescription="@string/app_name" />
<ImageView
android:id="@+id/iv3"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:contentDescription="@string/app_name" />
</LinearLayout>手順4:MainActivity.javaを実装する
次のコードを src/MainActivity.java に追加します。
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView iv1, iv2, iv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1 = findViewById(R.id.iv1);
iv2 = findViewById(R.id.iv2);
iv3 = findViewById(R.id.iv3);
// XMLで定義した配列リソースをTypedArrayとして取得
TypedArray images = getResources().obtainTypedArray(R.array.random_images);
// インデックスを指定してドローアブルリソースIDを取得し、ImageViewに設定
iv1.setImageResource(images.getResourceId(0, -1));
iv2.setImageResource(images.getResourceId(1, -1));
iv3.setImageResource(images.getResourceId(2, -1));
// TypedArrayは必ずrecycle()を呼び出して解放する
images.recycle();
}
}重要なのは getResources().obtainTypedArray() で配列リソースを取得し、getResourceId(インデックス, デフォルト値) を使って個々のリソースIDを取り出す部分です。また、TypedArray は使用後に必ず recycle() を呼び出してメモリを解放しましょう。
手順5: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端末をパソコンに接続していることを前提とします。Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルを開き、ツールバーの実行(Run)アイコンをクリックします。オプションとして自分のモバイルデバイスを選択すると、接続した端末の画面に以下のように画像が表示されます。

まとめ
このように、XML値ファイルに <integer-array> としてドローアブル参照を定義しておけば、TypedArray 経由で簡単にリソースIDを取得できます。画像リストを一元管理できるため、スライドショーやギャラリー機能など、複数の画像を扱うアプリで特に便利なテクニックです。ぜひ活用してみてください。
-
【Android】SharedPreferencesで値を保存・読み取り・編集する方法を徹底解説
SharedPreferencesとは? SharedPreferencesは、Androidアプリでキーと値(Key-Value)のペア形式の小さなデータを永続的に保存するための仕組みです。ユーザー名やメールアドレスなどの設定情報、ログイン状態、前回起動時の状態など、SQLiteデータベースを用意するまでもない軽量なデータの保存に最適で、多くのアプリで広く利用されています。 本記事では、実際に動作するサンプルコードをもとに、SharedPreferencesで値を「保存」「読み取り」「編集(クリア)」する方法をステップごとに解説します。 ステップ1:新規プロジェクトを作成する Android
-
【Android】XMLファイルを使ってアニメーションを作成する方法をわかりやすく解説
この記事では、AndroidアプリにおいてXMLファイルを使用してアニメーションを作成する方法を、実際のコード例とともに段階的に解説します。View Animation(Tween Animation)は、res/animディレクトリに配置したリソースファイルとして定義できるため、フェードインやズーム、点滅といった演出をJava側のコードをほとんど書かずに実現できるのが特徴です。 ステップ1:新規プロジェクトの作成 Android Studioを起動し、メニューから「File」⇒「New Project」を選択して新しいプロジェクトを作成します。ウィザードに従って必要な情報を入力し、プロジェク