Androidアプリで円形のカスタムプログレスバーを作成する方法をわかりやすく解説
Androidアプリ開発では、ダウンロードの進捗状況やデータ読み込みの状態などをユーザーに伝えるために、プログレスバー(ProgressBar)がよく使われます。本記事では、標準のプログレスバーをカスタマイズし、独自デザインの円形プログレスバーを実装する方法を、サンプルコード付きでステップごとに解説します。
実装の全体像
今回作成するのは、リング状(ドーナツ型)のカスタムプログレスバーです。実装のポイントは以下の2つです。
- レイアウトXMLでProgressBarウィジェットを配置し、カスタムのprogressDrawableを指定する
- shapeタグを使ったドローアブルXMLで、リング状の見た目を定義する
ステップ1:プロジェクトを新規作成する
まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」⇒「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成してください。
ステップ2:レイアウトファイル(activity_main.xml)にProgressBarを追加する
res/layout/activity_main.xml に以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <ProgressBar android:layout_width="150dp" android:layout_height="150dp" style="?android:progressBarStyleLarge" android:progress="50" android:id="@+id/progressBar" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:progressDrawable="@drawable/circle"/> </android.support.constraint.ConstraintLayout>
ここでのポイントは、android:progressDrawable="@drawable/circle" の指定です。これにより、デフォルトの見た目ではなく、次のステップで作成するカスタムドローアブルが描画されるようになります。また、ConstraintLayoutの制約を4方向に設定することで、プログレスバーが画面中央に配置されます。
ステップ3:カスタムドローアブル(circle.xml)を作成する
drawableリソースファイルとして circle.xml を新規作成し、以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="ring" android:innerRadiusRatio="2.5" android:thickness="4dp" android:useLevel="true"> <solid android:color="@color/colorAccent"/>
各属性の意味は次のとおりです。
- android:shape="ring":形状をリング(輪)状に指定します
- android:innerRadiusRatio="2.5":内側の穴の大きさを調整します。値が大きいほど中心の空洞が広くなります
- android:thickness="4dp":リングの線の太さを指定します
- android:useLevel="true":進捗度に応じて描画範囲が変化するようになります。falseにすると進捗に関係なく全周が表示されます
色は colorAccent を参照していますが、colors.xmlで定義されている任意の色に変更可能です。
ステップ4:MainActivity.javaにコードを追加する
src/MainActivity.java に以下のコードを記述します。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
progressBar.setMax(100);
progressBar.setProgress(20);
}
}setMax(100)で最大値を100に設定し、setProgress(20)で現在の進捗を20%にしています。この数値を変更すれば、表示される進捗量が変わります。実際のアプリでは、ダウンロード処理やAPI通信の完了率に応じてこの値を動的に更新します。
ステップ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スマートフォンをPCに接続しているものとします。Android Studioからプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。デバイス選択画面で接続したスマートフォンを選択すると、端末上にアプリが起動し、カスタマイズした円形プログレスバーが表示されます。

まとめ
ProgressBarのprogressDrawable属性に自作のドローアブルを指定するだけで、簡単にオリジナルデザインのプログレスバーを実装できます。色や太さ、内径の比率を調整すれば、アプリのテーマに合わせた表現も自由自在です。ぜひ自分のプロジェクトでも試してみてください。
-
Tkinterでダウンロードプログレスバーを実装する方法を解説
ファイルのダウンロードや進捗状況の追跡など、外部ソースやファイルとやり取りするアプリケーションを開発する場合、処理の進行状況をユーザーに視覚的に伝えるプログレスバーがあると便利です。Tkinterでプログレスバーを作成するには、tkinter.ttkパッケージに含まれるProgressbarモジュールを使用します。まずProgressbarオブジェクトをインスタンス化し、向き(orientation)をHORIZONTAL(水平方向)に設定します。その後、プログレスバーの値を増加させながら継続的に更新する関数を定義することで、ダウンロードの進捗を表現できます。サンプルコード以下の例では、プログ
-
Androidでカスタムジェスチャーを追加して操作を時短する方法
Androidデバイスにカスタムジェスチャーを設定すれば、日々のスマホ操作をぐっと快適にできます。自分が一番使いやすい動きで、新しいタブを開くなどの特定のタスクを実行できるように、端末を自由にカスタマイズできるのが魅力です。 例えば、画面の上端からスワイプするだけで最近使ったアプリへアクセスできたら便利だと思いませんか?カスタムジェスチャーがない状態では、クイック設定パネル( Wi-Fiや歯車アイコンなど)にしかアクセスできません。この記事では、代表的なカスタムジェスチャーアプリを紹介しながら、設定方法をわかりやすく解説します。 All in One Gesturesでカスタムジェスチャーを追