LoginSignup
24
24

More than 5 years have passed since last update.

Androidによるネイティブアプリ作成(スプラッシュ編)

Posted at

Androidによるネイティブアプリ作成(スプラッシュ編)

はじめに

PhoneGapでの記述で進めようと思いましたが、実装が2.0.0より上のバージョンから外部ドメインを直接読み込めないようになっていたので、ネイティブで書いたほうが早いんじゃね?
という理由でAndroidではPhoneGapを使用せず書くことにしました。コード量的にはこっちのほうがスッキリして管理できる範囲なのでこっちにしました。PhoneGapはバージョンごとにどこがどう変わったのかいちいち調べるのが面倒くさいです。用は楽してコード書きたいんです。人のオナニー他人に押し付けるなということです。
PhoneGap? (゚⊿゚)シラネ...

スプラッシュ表示

アプリで大概でてくるスプラッシュですが、実装は簡単です。
とはいってもXcodeみたく親切ではないです。

用意するものは端末サイズごとに3パターン用意します。
1パターンでもいいですがそうすると端末側で拡大縮小されるんで綺麗に見えません。
それぞれ、logo.pngとします。xhdpiはめんどいので...

・drawable-hdpi 480 x 800 px
・drawable-ldpi 240 x 400 px
・drawable-mdpi 320 x 480 px

Activity

Activityとは、“Androidアプリの画面”に相当します。ボタンやリストが配置された画面、Webページが表示されている画面、3Dグラフィックスが表示されている画面などはそれぞれがActivityです。Androidは画面を持たないアプリも作成可能で、その場合はActivityを使用しないものもあります。スプラッシュはアクティビティを使用します。今回は、resのlayoutに新規追加でsplash.xmlを追加してください。

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerInside"
        android:src="@drawable/logo"
    />
</LinearLayout>

ご覧のようにlogoを指定してフル表示指定しています。
次にクラスを書きます。今回は、srcにSplashActivity.javaとします。

SplashActivity.java

package jp.co.hogehoge.piyopiyo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class SplashActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);
        Handler hdl = new Handler();
        hdl.postDelayed(new splashHandler(), 500);

    }

    class splashHandler implements Runnable {
        public void run() {
            Intent i = new Intent(getApplication(),
                    SimpleActivity.class);
            startActivity(i);
            SplashActivity.this.finish();
        }
    }
}

ご覧のようにonCreateされた時点でタイトル非表示とし、splash.xmlを呼び出します。
ハンドラをインスタンスし、500ミリセカンド経過したのち、次のアクティビティに遷移します。

AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

AndroidManifest.xmlのintent-filterを書き換えます。
action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.LAUNCHER"
は初期起動時に表示するアクティビティを意味します。ひとつのアプリにこの表記はひとつだけです。
複数つけるとインストール時に同一アプリのアイコンが複数ウイルスのように表示されます...。
気をつけましょう。

まとめ

XCodeのようにIBを使用するまでもないですが、コードの読みやすさはさすがにObject-Cよりわかりやすいです。

24
24
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
24
24