Splash Screen Image for landscape and Portrait using Theme

强颜欢笑 提交于 2019-11-30 20:59:38

问题


I have two full screens splash images, one is for landscape and another is for portrait mode.I want to implemented theses images as splash as per the orientation of the device when app starts.

For Example if app starts from landscape mode than landscape image should be displayed as background image and if app starts from portrait mode than portrait image should be displayed as background image I have refer the following Example

This is how i setup the Theme:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_screen_1</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

I set this theme for splash activity from AndroidManifest.xml

    <activity
            android:name=".activities.SplashActivity"
            android:allowBackup="true"
            android:label="@string/app_name"
            android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Two show land and port images i have place two images with same name splash_screen_1.jpg images in drawable-land-hdpi and drawable-port-hdpi folders respectivley

Activity:

public class SplashActivity extends AppCompactActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        startApp();
    }

    private void startApp() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                    HomeActivity.startHomeActivity(SplashActivity.this);
                finish();
            }
        }, 2000);
    }
}

Now the problem is when app starts from portrait mode its works perfectly but when we start from landscape mode its start app with portrait image stretch out for few milliseconds and than it sets the landscape images like a glitch

I tried Following solutions Link 1 Link 2

But this solution is not suitable because it shows blanks screen in the beginning for few seconds to load the images and its takes little more time to load images from layout

Any alternate or a proper solution for this will be helpful


回答1:


this is not answer of your Question But ..

Spash screen is to wait till Activity Ready , Not to make user Wait more

so what about trying Diffrent way to make Splash screen , it`s built in

instead of white or black screen

Go to youe Manifest

change your app style like this

<activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/splashScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

where splashScreenTheme is

<style name="splashScreenTheme" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_screen</item>
    </style>

Sure you can change parent and make new drawable "Splashscreen" this is mine

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/holo_blue_light"/>
    <!-- Your product logo - 144dp color version of your app icon -->
    <item>
        <bitmap
            android:src="@drawable/splash_logo"
            android:gravity="center"/>
    </item>
</layer-list>

Now your app Style changed you can reset it in your main Activity Oncreate

@Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme222);
}



回答2:


If you must use theme approach then Please create a separate styles.xml file into new values-land/ folder into res folder...



来源:https://stackoverflow.com/questions/43042818/splash-screen-image-for-landscape-and-portrait-using-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!