SplashScreen 是中央裁剪。

huangapple go评论60阅读模式
英文:

SplashScreen is center cropping

问题

以下是您要翻译的内容:

对于我的应用程序,我正在使用一个可绘制资源文件创建一个启动屏幕,该文件包含一个图层列表,其中包括一个带有有色背景和居中的矢量可绘制图标的图层。

splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/grey_700" />
    <item
        android:drawable="@drawable/splash_foreground"
        android:gravity="center" />
</layer-list>

style.xml

...

<style name="Theme.SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:statusBarColor">@color/grey_900</item>
    <item name="android:windowBackground">@drawable/splash_screen</item>
</style>

...

AndroidManifest.xml

...

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:theme="@style/Theme.SplashScreen"
    android:screenOrientation="portrait"
    tools:ignore="LockedOrientationActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

...

splash_foreground.xml

SplashScreen 是中央裁剪。

问题
输出应该显示splash_foreground在屏幕中居中显示,而背景颜色应在整个屏幕上裁剪,但实际上显示的是一个居中裁剪的启动屏幕。

SplashScreen 是中央裁剪。

英文:

For my app I am creating an Splash Screen using a drawable resource file consist of a layerlist having a colored background and a centered vector drawable for the logo.

splash_screen.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item
        android:drawable=&quot;@color/grey_700&quot; /&gt;
    &lt;item
        android:drawable=&quot;@drawable/splash_foreground&quot;
        android:gravity=&quot;center&quot; /&gt;
&lt;/layer-list&gt;

style.xml

...

&lt;style name=&quot;Theme.SplashScreen&quot; parent=&quot;Theme.MaterialComponents.DayNight.NoActionBar&quot;&gt;
    &lt;item name=&quot;android:statusBarColor&quot;&gt;@color/grey_900&lt;/item&gt;
    &lt;item name=&quot;android:windowBackground&quot;&gt;@drawable/splash_screen&lt;/item&gt;
&lt;/style&gt;

...

AndroidManifest.xml

...

        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:exported=&quot;true&quot;
            android:theme=&quot;@style/Theme.SplashScreen&quot;
            android:screenOrientation=&quot;portrait&quot;
            tools:ignore=&quot;LockedOrientationActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

...

splash_foreground.xml

SplashScreen 是中央裁剪。

Problem

the output should show splash_foreground centered in the screen while the background color is cropped all over the screen but is showing a splash screen centered cropped.

SplashScreen 是中央裁剪。

答案1

得分: 1

I solved this problem by using the new splash_screen API. all thanks to this post

Solution

  • Add Splash Screen API in build.gradle
    implementation 'androidx.core:core-splashscreen:1.0.0'

  • Add this line before the application tag in the AndroidManifest.xml file
    <uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />

  • In theme.xml & theme.xml (night) create a new style with Theme.SplashScreen being parent

        <item name="android:statusBarColor">@color/grey_900</item> 
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item> 
        <item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item> 
        <item name="windowSplashScreenBackground">@color/grey_700</item> 
        <item name="postSplashScreenTheme">@style/Theme.LGConnect</item> 
    </style> ```

- **Set application theme and Launcher Activity theme to the newly created style**
``` ...
<application
...
android:theme="@style/Theme.App.Starting"
...
<activity
...
android:theme="@style/Theme.App.Starting"
... ```

- **Inside `onCreate` method of `MainActivity` add this line**
``` ...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
... ```


[1]: https://stackoverflow.com/a/72320117/16177121

<details>
<summary>英文:</summary>

I solved this problem by using the new splash_screen API. all thanks to [this post][1]

## Solution ##
- **Add Splash Screen API in `build.gradle`**

implementation 'androidx.core:core-splashscreen:1.0.0'


- **Add this line before the application tag in the `AndroidManifest.xml` file**

<uses-sdk tools:overrideLibrary="androidx.core.splashscreen" />


- **In `theme.xml` &amp; `theme.xml (night)` create a new style with `Theme.SplashScreen` being parent**

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="android:statusBarColor">@color/grey_900</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_app_foreground</item>
<item name="windowSplashScreenIconBackgroundColor">@color/grey_700</item>
<item name="windowSplashScreenBackground">@color/grey_700</item>
<item name="postSplashScreenTheme">@style/Theme.LGConnect</item>
</style>

- **Set application theme and Launcher Activity theme to the newly created style**

...
<application
...
android:theme="@style/Theme.App.Starting"
...
<activity
...
android:theme="@style/Theme.App.Starting"
...


- **Inside `onCreate` method of `MainActivity` add this line**

...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen()
...


  [1]: https://stackoverflow.com/a/72320117/16177121

</details>



huangapple
  • 本文由 发表于 2023年4月13日 16:21:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定