【Android】Drawableの色を変更する方法を初心者向けに解説
はじめに
この記事では、AndroidアプリでDrawable(ドローアブル)の色を変更する方法を、実際のコード例を交えながらステップごとに解説します。サンプルでは AnimationDrawable を活用し、背景のグラデーションが滑らかにフェードしながら切り替わるアニメーションを実装します。
ステップ1:新規プロジェクトを作成する
Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成してください。
ステップ2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" android:id="@+id/activity_main" android:orientation="vertical" tools:context=".MainActivity" tools:ignore="InvalidId"> </LinearLayout>
ここでポイントとなるのは、android:background 属性にカスタムDrawable(@drawable/background)を指定している点です。この背景に対して後ほどアニメーションを適用します。
ステップ3:MainActivity.java を編集する
src/MainActivity.java に以下のコードを追加します。
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout your_Layout = (LinearLayout) findViewById(R.id.activity_main);
AnimationDrawable animationDrawable = (AnimationDrawable) your_Layout.getBackground();
animationDrawable.setEnterFadeDuration(4000);
animationDrawable.setExitFadeDuration(4000);
animationDrawable.start();
}
}レイアウトの背景を AnimationDrawable として取得し、setEnterFadeDuration() と setExitFadeDuration() でフェードイン・フェードアウトの時間(ミリ秒)を設定しています。最後に start() を呼び出すことでアニメーションが開始されます。
ステップ4:アニメーション定義ファイル(background.xml)を作成する
res/drawable/background.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/gradient1" android:duration="4000" /> <item android:drawable="@drawable/gradient2" android:duration="4000" /> </animation-list>
<animation-list> 要素を使うことで、複数のDrawableを順番に表示するフレームアニメーションを定義できます。ここでは gradient1 と gradient2 をそれぞれ4秒間隔で切り替える設定にしています。
ステップ5:1つ目のグラデーション(gradient1.xml)を作成する
res/drawable/gradient1.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <gradient android:angle="225" android:endColor="#044fab" android:startColor="#21d6d3" /> </shape>
ステップ6:2つ目のグラデーション(gradient2.xml)を作成する
res/drawable/gradient2.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <gradient android:angle="45" android:endColor="#933c94" android:startColor="#517f95" /> </shape>
<gradient> の startColor と endColor の値を変更すれば、お好みの配色に自由にカスタマイズできます。angle を変えればグラデーションの方向も調整可能です。
ステップ7:AndroidManifest.xml を確認・編集する
Manifests/AndroidManifest.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.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」アイコンをクリックします。デバイス選択のダイアログが表示されたら、接続したモバイルデバイスを選択してください。しばらくすると、スマートフォンの画面にグラデーションが滑らかに切り替わる背景アニメーションが表示されます。

-
Androidのキーボードを変更する方法を徹底解説!機種別の手順とおすすめアプリもご紹介
Android OSの大きな魅力のひとつは、自由度の高いカスタマイズ性です。その代表格ともいえるのが、スマートフォンの初期搭載(ストック)キーボードを好みのものへ変更できる機能です。この変更は、Samsung、Google、Huawei、Xiaomiなど、端末のブランドを問わず行うことができます。多くの純正キーボードは十分に優秀ですが、ユーザーのニーズによっては別のキーボードが必要になることもあります。例えば、他の言語で入力したい場合や、数式で数学記号を使いたい場合など、標準キーボードが対応していない機能が必要なケースです。Androidキーボードの変更手順は基本的にどの端末でも共通しています
-
Android 6.0でUSB設定を変更する方法|開発者向けオプションの使い方を徹底解説
Android 6.0(Marshmallow)は、多くの最新デバイスと互換性のあるプラットフォームです。一部のメーカーではすでにアップデートの配信が始まっていますが、いまだにこのバージョンへ更新されていないスマートフォンも少なくありません。お使いのAndroidスマートフォンがAndroid 6.0にアップグレードされている場合、PCに接続するとUSB設定が初期状態で「充電モード」になっていることに気づいた方も多いのではないでしょうか。このUSB設定を変更したい方は、ぜひ本記事をご覧ください。AndroidでUSB設定を変更する方法を、初心者にもわかりやすく解説します。 Android