728x90
728x170

어플 실행 시 splash screen 을 보여주고 싶어 방법을 찾아보니 아래 출처에 잘 소개가 되어있어 소개합니다.

출처 : https://forums.xamarin.com/discussion/19362/xamarin-forms-splashscreen-in-android

 

우선 splash 에 보여줄 이미지를 준비합니다.

아래 예시에서 splash 에 사용될 이미지 명은 'icon' 입니다.

우선 Activity 를 만들어 줍니다. ( SplashScreen )

아래 내용은 Theme = "@style/Theme.Splash" 여기에 정의된 테마화면을 보여주고 난다음

메인 화면인 MainActivity 를 호출하고 자기자신은 Finish() 하여 Splash 화면을 닫아버립니다.

SplashScreen.cs:

    [Activity(Label = "MyApp", MainLauncher = true, NoHistory = true, Theme = "@style/Theme.Splash",
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class SplashScreen : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            var intent = new Intent(this, typeof(MainActivity));
            StartActivity(intent); Finish();
        }
    }

아래가 실제 메인 화면입니다.

MainActivity.cs:

[Activity(Label = "MyApp", Theme = "@android:style/Theme.Holo", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : AndroidActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);       
           
            // Xamarin Forms
            Xamarin.Forms.Forms.Init(this, bundle);
            SetPage(App.GetMainPage());

            // Xamarin Android
            SetContentView(Resource.Layout.Main);
        }
    }

아래는 실제 화면에 보여질 이미지의 속성을 정의한다고 보면 될것 같습니다.

"@drawable/icon" 이 항목이 실제 화면에 보여질 이미지에 해당됩니다.

아래 두항목(gravity,layout_gravity) 은 가운데 이미지를 위치시키위한 내용입니다.

Resources.Drawable.SplashScreen.xml:

<?xml version="1.0" encoding="utf-8" ?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/icon" 
        android:gravity="center"
        android:layout_gravity="center"/>

아래는 화면 구조를 정의하는 내용입니다.

windowNoTitle 가 true 로 되어야 상단에 toolbar 가 사라지게됩니다.

Resources.Values.Styles.xml

<resources>
  <style name="Theme.Splash"    parent="android:Theme">
    <item name="android:windowBackground">
      @drawable/splashscreen
    </item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
  </style>
</resources>

 

이렇게 하고 시뮬레이터로 동작 시키게되면 어플이 실행되기전에

스플래시이미지가 보인후 메인 화면으로 진입하게 됩니다.

 

아래는 제가 실제 적용한 내용입니다.

위 내용과는 조금 다른 부분이있습니다.

SplashActivity.cs

[Activity(Label = "SMSConvey", Theme = "@style/SplashTheme", MainLauncher = true, NoHistory = true, Icon = "@drawable/conveysmsicon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class SplashActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Thread thread = new Thread(() =>
            {
                this.RunOnUiThread(() =>
                SimulateStartup());
            }
            );
            thread.Start();

        }

        // Launches the startup task
        protected override void OnResume()
        {
            base.OnResume();
        }

        // Prevent the back button from canceling the startup process
        public override void OnBackPressed() { }

        // Simulates background work that happens behind the splash screen
        private void SimulateStartup()
        {
            //await Task.Delay(1000); // Simulate a bit of startup work.
            Thread.Sleep(500);
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            Finish();
        }
    }

Styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="SplashTheme" parent ="@android:Theme">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">true</item>
  </style>
</resources>

splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="#FFFFFF"/>
  </item>
  <item>
    <bitmap
        android:src="@drawable/smsconvey_splash"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

참고

https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

http://www.c-sharpcorner.com/UploadFile/1e050f/creating-splash-screen-for-android-app-in-xamarin/

728x90
그리드형

'C# > Xamarin Maui' 카테고리의 다른 글

(Xamarin Forms) StartTimer  (0) 2017.06.22
(Xamarin) WiFi 접속하기  (0) 2017.06.18
(Xamarin.Android) AdMob 광고 넣기 (2)  (3) 2017.06.15
(Xamarin.Android) AdMob 광고 넣기 (1)  (0) 2017.06.15
(Xamarin) Error : TargetFrameworkVersion  (0) 2017.06.13
Posted by kjun
,