Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完全模仿微信字体设置.md #1

Open
johnwatsondev opened this issue Mar 5, 2016 · 0 comments
Open

完全模仿微信字体设置.md #1

johnwatsondev opened this issue Mar 5, 2016 · 0 comments

Comments

@johnwatsondev
Copy link
Owner

行为


使用 adb shell dumpsys activity activities 命令查看 activities 在任务栈变化,
改进版命令:adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p' (多谢前辈 @liuxing917738 指导)

测试机型:Nexus 5 (6.0.1)

Task Stack 变化

Step 1 2 3 4 均为手动添加,方便下文叙述。

Step 1

Running activities (most recent first):
    TaskRecord{45a6662 #208 A=com.tencent.mm U=0 sz=4}
      Run #3: ActivityRecord{c04e6f1 u0 com.tencent.mm/.plugin.setting.ui.setting.SettingsFontUI t208}
      Run #2: ActivityRecord{8fa53ac u0 com.tencent.mm/.plugin.setting.ui.setting.SettingsAboutSystemUI t208}
      Run #1: ActivityRecord{9c0eebd u0 com.tencent.mm/.plugin.setting.ui.setting.SettingsUI t208}
      Run #0: ActivityRecord{d29fc3a u0 com.tencent.mm/.ui.LauncherUI t208}

Step 2

Running activities (most recent first):
    TaskRecord{477c03b #214 A=com.tencent.mm U=0 sz=1}
      Run #2: ActivityRecord{44c7cbe u0 com.tencent.mm/.ui.LauncherUI t214}
    TaskRecord{45a6662 #208 A=com.tencent.mm U=0 sz=2}
      Run #1: ActivityRecord{d29fc3a u0 com.tencent.mm/.ui.LauncherUI t208 f}
      Run #0: ActivityRecord{c04e6f1 u0 com.tencent.mm/.plugin.setting.ui.setting.SettingsFontUI t208 f}

Step 3

Running activities (most recent first):
    TaskRecord{477c03b #214 A=com.tencent.mm U=0 sz=1}
      Run #1: ActivityRecord{44c7cbe u0 com.tencent.mm/.ui.LauncherUI t214}
    TaskRecord{45a6662 #208 A=com.tencent.mm U=0 sz=1}
      Run #0: ActivityRecord{c04e6f1 u0 com.tencent.mm/.plugin.setting.ui.setting.SettingsFontUI t208 f}

Step 4

Running activities (most recent first):
    TaskRecord{477c03b #214 A=com.tencent.mm U=0 sz=1}
      Run #0: ActivityRecord{44c7cbe u0 com.tencent.mm/.ui.LauncherUI t214}
各 Activity 清单文件如下:
<activity android:configChanges="keyboardHidden|orientation|screenSize" android:label="@string/axo" android:launchMode="singleTop" android:name=".ui.LauncherUI" android:theme="@style/iz" android:windowSoftInputMode="adjustResize|stateHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.MULTIWINDOW_LAUNCHER"/>
    </intent-filter>
    <intent-filter>
        <action android:name="com.tencent.mm.action.BIZSHORTCUT"/>
        <category android:name="android.intent.category.
    </intent-filter>
</activity>

<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name=".plugin.setting.ui.setting.SettingsFontUI"/>

<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name=".plugin.setting.ui.setting.SettingsAboutSystemUI"/>

<activity android:configChanges="keyboardHidden|orientation|screenSize" android:name=".plugin.setting.ui.setting.SettingsUI"/>
TL;DR : 各 Activity LaunchMode 如下:

LauncherUI :singleTop
SettingsFontUI:standand
SettingsAboutSystemUI:standand
SettingsUI:standand

已实现的方案


在 SettingsFontUI 界面中捕获点击 back 键事件,加入如下代码:

  @Override public void onBackPressed() {
    //super.onBackPressed();

    // Restart SettingsFontUI activity if font scale changed
    Intent i = new Intent(SettingsFontUI.this, LauncherUI.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
  }

修改 font scale 代码:

public static void setFontScale(Context context, float scale) {
    Context appContext =   context.getApplicationContext();

    Configuration configuration = appContext.getResources().getConfiguration();

    Configuration newConfiguration = new Configuration();
    newConfiguration.updateFrom(configuration);
    newConfiguration.fontScale = scale;

    DisplayMetrics newMetrics = new DisplayMetrics();
    newMetrics.setToDefaults();

    context.getApplicationContext()
        .getResources()
        .updateConfiguration(newConfiguration, newMetrics);

    // save font scale in SharedPreferences or DB (anywhere you want)
  }

当 LauncherUI 启动时,直接读取 font scale 并在 onCreate 方法中设置即可。

分析


  1. 观察之后发现,从 Step 1 到 2 的变化特别有趣,在 Task 208 中,栈底 Activity 从 LauncherUI 变为 SettingsFontUI,中间的两个界面消失,然后从 Step 2 到 4,LauncherUI 和 SettingsFontUI 依次消失。
  2. 新的 LauncherUI 是在新的 Task 214 中出现的。

疑问


在新 Task 中启动主界面,然后把旧 Task 中 所有 Activity 全部结束,这是如何做到的?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant