Поиск по этому блогу

воскресенье, 2 февраля 2014 г.

Android. Добавляем splash-screen к нашему приложению

Создание экрана загрузки (Splash Screen) для Android-приложения при помощи таймера. 


1. Для создания экрана загрузки (Splash Screen Activity) создадим класс SplashScreenActivity.java

2. Откроем наш AndroidManifest.xml файл и убедимся, что splash screen activity является стартовой активити проекта.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="pro.newapp.MarkXWallpapers"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>
    <application android:label="@string/app_name" 
        android:icon="@drawable/icon_markx">
        <activity android:name="MainActivity"
                  android:label="@string/app_name"
                  android:configChanges="orientation"
                  android:screenOrientation="portrait">
        </activity>
        <activity android:name=".SplashScreenActivity"
                  android:configChanges="orientation"
                  android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        </activity>
    </application>
</manifest>

3. Создадим layout-файл для заставки (для splash screen) в папке res/layout. Я назвал этот файл splash_screen.xml. В мое заставке содержится изображение из приложения и логотип нашей компании, а также адрес сайта.

<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
 
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/new_app_logo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"/>
 
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/markx_logo"
            android:layout_centerInParent="true"
            android:layout_weight="1"/>
 
    <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
              android:text="@string/new_app_site"
              android:textSize="22dp"
              android:layout_marginBottom="20dp"
              android:height="50dp"/>
 
</LinearLayout>

4. Добавим следующий код в класс SplashScreenActivity.java. В этом фрагменте кода используется handler, который будет ждать в течении заданного времени и по истечению таймера будет запускаться главная активити проекта.

public class SplashScreenActivity extends Activity {
 
    private static int SPLASH_SCREEN_TIMEOUT = 2500;
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.splash_screen);
 
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, SPLASH_SCREEN_TIMEOUT);
    }
 
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
 
}

Как это работает в настоящем приложении вы можете посмотреть тут



Комментариев нет:

Отправить комментарий