AndroidAutoSize icon indicating copy to clipboard operation
AndroidAutoSize copied to clipboard

启动页Manifest中配置的theme,无法适配

Open snowberry001 opened this issue 4 years ago • 3 comments

Environment

  • [x] Autosize Version: 1.2.1
  • [x] Target Android Version: 23
  • [x] Device Model: All
  • [x] Device Resolution: All
  • [x] Design Size On AndroidManifest: 基于宽度 375dp

问题描述

启动页在AndroidManifest中设置了自定义的theme,大致就是logo+slogan这样的背景,用来优化启动的用户体验的,现在发现背景中的图片都不会自动适配。

原因分析

startActivity时在Activity的第一帧渲染之前,系统会自动添加一个startingWindow,这个window创建时会应用配置的theme,但是应用启动时使用的相关context关联的DisplayMetric没有进行适配,导致这个现象。

解决方案

目前没有想到很好的解决方案 - -!特来求助

Related Code:

// PhoneWindowManager.java
public StartingSurface addSplashScreen(IBinder appToken, String packageName, int theme,
            CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes, int icon,
            int logo, int windowFlags, Configuration overrideConfig, int displayId) {

// 省略。。。
Context context = mContext;
            if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen " + packageName
                    + ": nonLocalizedLabel=" + nonLocalizedLabel + " theme="
                    + Integer.toHexString(theme));

            // Obtain proper context to launch on the right display.
            final Context displayContext = getDisplayContext(context, displayId);
            if (displayContext == null) {
                // Can't show splash screen on requested display, so skip showing at all.
                return null;
            }
            context = displayContext;

            if (theme != context.getThemeResId() || labelRes != 0) {
                try {
                    context = context.createPackageContext(packageName, CONTEXT_RESTRICTED);
                    context.setTheme(theme);
                } catch (PackageManager.NameNotFoundException e) {
                    // Ignore
                }
            }

            if (overrideConfig != null && !overrideConfig.equals(EMPTY)) {
                if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen: creating context based"
                        + " on overrideConfig" + overrideConfig + " for splash screen");
                final Context overrideContext = context.createConfigurationContext(overrideConfig);
                overrideContext.setTheme(theme);
                final TypedArray typedArray = overrideContext.obtainStyledAttributes(
                        com.android.internal.R.styleable.Window);
                final int resId = typedArray.getResourceId(R.styleable.Window_windowBackground, 0);
                if (resId != 0 && overrideContext.getDrawable(resId) != null) {
                    // We want to use the windowBackground for the override context if it is
                    // available, otherwise we use the default one to make sure a themed starting
                    // window is displayed for the app.
                    if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "addSplashScreen: apply overrideConfig"
                            + overrideConfig + " to starting window resId=" + resId);
                    context = overrideContext;
                }
                typedArray.recycle();
            }

            final PhoneWindow win = new PhoneWindow(context);
            win.setIsStartingWindow(true);

            CharSequence label = context.getResources().getText(labelRes, null);
            // Only change the accessibility title if the label is localized
            if (label != null) {
                win.setTitle(label, true);
            } else {
                win.setTitle(nonLocalizedLabel, false);
            }

            win.setType(
                WindowManager.LayoutParams.TYPE_APPLICATION_STARTING);

            synchronized (mWindowManagerFuncs.getWindowManagerLock()) {
                // Assumes it's safe to show starting windows of launched apps while
                // the keyguard is being hidden. This is okay because starting windows never show
                // secret information.
                if (mKeyguardOccluded) {
                    windowFlags |= FLAG_SHOW_WHEN_LOCKED;
                }
            }

            // Force the window flags: this is a fake window, so it is not really
            // touchable or focusable by the user.  We also add in the ALT_FOCUSABLE_IM
            // flag because we do know that the next window will take input
            // focus, so we want to get the IME window up on top of us right away.
            win.setFlags(
                windowFlags|
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                windowFlags|
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

            win.setDefaultIcon(icon);
            win.setDefaultLogo(logo);

            win.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT);

            final WindowManager.LayoutParams params = win.getAttributes();
            params.token = appToken;
            params.packageName = packageName;
            params.windowAnimations = win.getWindowStyle().getResourceId(
                    com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
            params.privateFlags |=
                    WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED;
            params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;

            if (!compatInfo.supportsScreen()) {
                params.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW;
            }

            params.setTitle("Splash Screen " + packageName);
            addSplashscreenContent(win, context);

            wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
            view = win.getDecorView();

            if (DEBUG_SPLASH_SCREEN) Slog.d(TAG, "Adding splash screen window for "
                + packageName + " / " + appToken + ": " + (view.getParent() != null ? view : null));

            wm.addView(view, params);
// 省略。。。

}

//AndroidManifest
<activity android:name=".MainActivity" android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>  


// MainTheme
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white"/>

    <item
        android:top="100dp">
        <bitmap
            android:gravity="top|center_horizontal"
            android:src="@drawable/logo"/>
    </item>

    <item
        android:bottom="50dp">
        <bitmap
            android:gravity="bottom|center_horizontal"
            android:src="@drawable/slogan"/>
    </item>

</layer-list>

Others:

snowberry001 avatar Jun 08 '20 06:06 snowberry001

框架只会在 Activity onCreate 设置 density, 启动图加载的时间明显早于 Activity onCreate,所以要想适配启动图就要在它加载之前设置 density,现在能想到的方法就是下面的方法,如果不行那就没办法了。

https://github.com/JessYanCoding/AndroidAutoSize/issues/1#issuecomment-504654664

根据上面的的回答,创建一个重写 getResources 方法的 ContextWrapper,

  @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(ContextWrapper);
    }

然后将这个 ContextWrapper 传给 Application 的 super.attachBaseContext();

JessYanCoding avatar Jun 16 '20 04:06 JessYanCoding

这个方法不行。只能曲线绕过了

snowberry001 avatar Jul 02 '20 01:07 snowberry001

launcher启动我们的应用Activity时,使用的是launcher对应的context,这个没有办法去修改,尴尬

snowberry001 avatar Jul 02 '20 01:07 snowberry001