LoginSignup
22
19

More than 5 years have passed since last update.

Fragmentを使ってSplashScreenを表示させる

Last updated at Posted at 2014-08-23

はじめに

SplashScreen実装するなら、アンドロイドアプリのスプラッシュ画面の実装例でばっちり。

せっかくまとめたので、参考程度にどうぞ。

SplashScreen用のFragment作成

DialogFragmentを継承したSplashScreenFragment.javafragment_splash_screen.xmlを作成する。

コードの概要は、下記のとおり。

  • フラグメントのサイズを画面表示領域の8割とする(任意)
  • Dialogをキャンセルできないようにする
  • DialogにSplashDialogThemeを適用する
  • layout内でsplashscreen用のImageViewタグを作成
  • splash.pngを作成し、drawalbeフォルダへ入れる
SplashScreenFragment.java
public class SplashScreenFragment extends DialogFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Dialog dialog = getDialog();

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int dialogWidth = (int) (metrics.widthPixels * 0.8);
        int dialogHeight = (int) (metrics.heightPixels * 0.8);

        lp.width = dialogWidth;
        lp.height = dialogHeight;
        dialog.getWindow().setAttributes(lp);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.SplashDialogTheme);
        // ダイアログをキャンセルできないようにする
        setCancelable(false);

        return dialog;
    }
}
fragment_splash_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="center_horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/splash" />

</LinearLayout>

styles.xmlにSplashDialogThemeの追加

Splash画面のみを表示させるため、下記のようなThemeとする。

コードの概要は、下記のとおり。

  • ActionBarの無効
  • Titleの非表示
styles.xml
    <style name="SplashDialogTheme" parent="@android:style/Theme.Holo.Dialog">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>

SpalashScreenFragmentを呼び出したいActivityを編集

SplashScreenFragmentを2秒表示させてから、Fragmentを閉じるため、openSplashScreenFragment()onCreate()に追加する。

SplashScreen.java
public class SplashScreen extends Activity {

    SplashScreenFragment dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.SplashDialogTheme);
        setContentView(R.layout.activity_splash_screen);

        openSplashScreenFragment();

    }

    private void openSplashScreenFragment(){
        dialog = new SplashScreenFragment();
        dialog.show(getFragmentManager(), "dialog");

        Handler handler = new Handler();

        handler.postDelayed(new Runnable()
        {
            @Override
            public void run() {

                dialog.dismiss();
            }

        }, 2000);
    }
}
22
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
19