【Android】実行時にViewのサイズ(幅・高さ)を取得する方法をわかりやすく解説
この記事では、Androidアプリの実行時にViewのサイズ(幅・高さ)を動的に取得する方法を、サンプルコード付きで段階的に解説します。
レイアウトが描画される前は getWidth() や getHeight() を呼んでも正しい値が取得できないため、ViewTreeObserver の OnGlobalLayoutListener を利用して、レイアウト完了後にサイズを取得するのがポイントです。
手順1:新規プロジェクトを作成する
Android Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力して新しいプロジェクトを作成します。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。ImageViewと、サイズ表示用のTextViewを配置しています。
<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:padding="8dp"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center"
android:layout_margin="20sp"
android:src="@drawable/image"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20sp"
android:textSize="16sp"
android:textStyle="bold"
android:layout_below="@id/imageView"/>
</RelativeLayout>手順3:画像ファイルを配置する
任意の画像ファイル(.png / .jpg / .jpeg)を res/drawable フォルダにコピー&ペーストしてください。ここではファイル名を「image」としています。
手順4:MainActivity.java にコードを追加する
src/MainActivity.java に以下のコードを記述します。getViewTreeObserver().addOnGlobalLayoutListener() を使うことで、画面のレイアウトが確定したタイミングでViewの幅と高さを取得できます。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new GetViewSize());
}
class GetViewSize implements ViewTreeObserver.OnGlobalLayoutListener {
@Override
public void onGlobalLayout() {
View v = findViewById(R.id.imageView);
String x = Integer.toString(v.getWidth());
String y = Integer.toString(v.getHeight());
((TextView)findViewById(R.id.textView)).setText(String.format("Width %s, Height: %s", x, y));
}
}
}補足:リスナーの解除について
OnGlobalLayoutListener はレイアウトのたびに何度も呼ばれる可能性があります。サイズ取得後に不要な場合は、imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this) を呼び出してリスナーを解除しておくと、無駄な処理を防げます。
手順5:AndroidManifest.xml を確認する
androidManifest.xml には以下のようにMainActivityを登録します。特別なパーミッションは不要です。
<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」アイコンをクリックしてください。実行デバイスとして接続したスマートフォンを選択すると、端末に以下のような画面が表示されます。

このように、ImageViewの下に配置されたTextViewに「Width ○○, Height: ○○」という形式で、実行時に取得したViewの幅と高さが表示されれば成功です。
-
【Android】実行時にViewのサイズ(幅・高さ)を取得する方法をわかりやすく解説
この記事では、Androidアプリの実行時にViewのサイズ(幅・高さ)を動的に取得する方法を、サンプルコード付きで段階的に解説します。レイアウトが描画される前は getWidth() や getHeight() を呼んでも正しい値が取得できないため、ViewTreeObserver の OnGlobalLayoutListener を利用して、レイアウト完了後にサイズを取得するのがポイントです。手順1:新規プロジェクトを作成するAndroid Studioを起動し、「File」→「New Project」を選択して、必要な項目を入力して新しいプロジェクトを作成します。手順2:レイアウトファイ
-
【Android】ImageViewで画像をアスペクト比を維持したまま拡大縮小して表示する方法
AndroidでImageViewの画像をアスペクト比を維持したまま拡大縮小する方法 この記事では、AndroidアプリでImageViewに表示した画像を、アスペクト比(縦横比)を維持しながら拡大・縮小して表示する方法を、サンプルコードとともにわかりやすく解説します。 ステップ1:新しいプロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。プロジェクト名や保存場所など必要な情報を入力して、新規プロジェクトを作成してください。 ステップ2:レイアウトファイルにImageViewを追加する res/layout/acti