activity 开始时黑屏 5-10 秒

Black screen for 5-10 seconds on activity start

我正在使用 Rich edit 文本库在 android 的编辑文本中创建 HTML。这是项目的 GitHub link。

Android-RTEditor

根据演示,我在 Activity 启动时注册了 RTApi 和 RTManager,但它在启动时增加了几秒钟的延迟,并出现黑屏。注册代码是

RTApi rtApi = new RTApi(this, new RTProxyImpl(this), new RTMediaFactoryImpl(this, true));
RTManager rtManager = new RTManager(rtApi, savedInstanceState);

这是 activity 开始时的 logcat

E/AssetIndex: assets.index
E/AssetIndex: java.io.FileNotFoundException: assets.index
E/AssetIndex:     at android.content.res.AssetManager.openAsset(Native Method)
E/AssetIndex:    at android.content.res.AssetManager.open(AssetManager.java:334)
E/AssetIndex:    at android.content.res.AssetManager.open(AssetManager.java:308)
E/AssetIndex: at com.onegravity.rteditor.fonts.AssetIndex.getAssetIndex(AssetIndex.java:46)
E/AssetIndex: at com.onegravity.rteditor.fonts.FontManager.getAssetFonts(FontManager.java:156)
E/AssetIndex:     at com.onegravity.rteditor.fonts.FontManager.getFonts(FontManager.java:97)
E/AssetIndex:     at com.onegravity.rteditor.toolbar.HorizontalRTToolbar.getFontItems(HorizontalRTToolbar.java:213)
E/AssetIndex:     at com.onegravity.rteditor.toolbar.HorizontalRTToolbar.onFinishInflate(HorizontalRTToolbar.java:177)
E/AssetIndex:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:814)
E/AssetIndex:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
E/AssetIndex:     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
E/AssetIndex:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
 E/AssetIndex:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
E/AssetIndex:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
E/AssetIndex:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
E/AssetIndex:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
E/AssetIndex:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
E/AssetIndex:     at com.workmatec.apps.androidapp.UIElements.AppFormActivity.onCreate(AppFormActivity.java:91)
E/AssetIndex:     at android.app.Activity.performCreate(Activity.java:6020)
E/AssetIndex:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
E/AssetIndex:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
E/AssetIndex:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
E/AssetIndex:     at android.app.ActivityThread.access0(ActivityThread.java:149)
E/AssetIndex:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
E/AssetIndex:     at android.os.Handler.dispatchMessage(Handler.java:102)
E/AssetIndex:     at android.os.Looper.loop(Looper.java:135)
E/AssetIndex:     at android.app.ActivityThread.main(ActivityThread.java:5299)
E/AssetIndex:     at java.lang.reflect.Method.invoke(Native Method)
E/AssetIndex:     at java.lang.reflect.Method.invoke(Method.java:372)
E/AssetIndex:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
E/AssetIndex:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
E/Minikin: addFont failed to create font /system/fonts/jb

这是我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.workmatec.apps.androidapp.UIElements.AppFormActivity">

<include
    android:id="@+id/rte_toolbar_container"
    layout="@layout/rte_toolbar" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/rte_toolbar_container">

    <LinearLayout
        android:id="@+id/appForm"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_background"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

    </LinearLayout>
</ScrollView>

XML 共 RTE_toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="?attr/actionBarItemBackground"
    android:scrollbarStyle="outsideOverlay" >

    <com.onegravity.rteditor.toolbar.HorizontalRTToolbar
        android:id="@+id/rte_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?attr/actionBarItemBackground"
        android:orientation="horizontal" >

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_bold"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarBold" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_italic"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarItalic" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_underline"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarUnderline" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_strikethrough"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarStrikethrough" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_superscript"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarSuperscript" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_subscript"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarSubscript" />

        <include layout="@layout/rte_toolbar_divider"/>

        <Spinner
            android:id="@+id/toolbar_font"
            style="?attr/rte_ToolbarSpinner"
            android:background="?attr/rte_icToolbarFont" />

        <Spinner
            android:id="@+id/toolbar_fontsize"
            style="?attr/rte_ToolbarSpinner"
            android:background="?attr/rte_icToolbarFontSize" />

        <Spinner
            android:id="@+id/toolbar_fontcolor"
            style="?attr/rte_ToolbarSpinner"
            android:background="?attr/rte_icToolbarFontColor" />

        <Spinner
            android:id="@+id/toolbar_bgcolor"
            style="?attr/rte_ToolbarSpinner"
            android:background="?attr/rte_icToolbarBGColor" />

        <include layout="@layout/rte_toolbar_divider"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_number"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarNumber" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_bullet"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarBullet" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_inc_indent"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarIncIndent" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_dec_indent"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarDecIntent" />

        <include layout="@layout/rte_toolbar_divider"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_align_left"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarAlignLeft" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_align_center"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarAlignCenter" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_align_right"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarAlignRight" />

        <include layout="@layout/rte_toolbar_divider"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_link"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarLink" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_image"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarImage" />

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_image_capture"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarCapture" />

        <!-- com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_audio"
            style="@style/ToolbarButton"
            android:background="?attr/rte_icToolbarAudio" /-->

        <!-- com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_video"
            style="@style/ToolbarButton"
            android:layout_marginRight="4dip"
            android:background="?attr/rte_icToolbarVideo" /-->

        <include layout="@layout/rte_toolbar_divider"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_undo"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarUndo"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_redo"
            style="@style/RTE_ToolbarButton"
            android:background="?attr/rte_icToolbarRedo"/>

        <com.onegravity.rteditor.toolbar.RTToolbarImageButton
            android:id="@+id/toolbar_clear"
            style="@style/RTE_ToolbarButton"
            android:layout_marginRight="4dip"
            android:background="?attr/rte_icToolbarClear"/>

    </com.onegravity.rteditor.toolbar.HorizontalRTToolbar>

</HorizontalScrollView>

RTE 工具栏按钮样式

<style name="RTE_ToolbarButton" parent="@android:style/Widget.ImageButton">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_gravity">center</item>
    <item name="android:layout_marginTop">4dip</item>
    <item name="android:layout_marginBottom">4dip</item>
    <item name="android:layout_marginLeft">4dip</item>
    <item name="android:gravity">center</item>
    <item name="android:adjustViewBounds">true</item>
</style>

我遇到了同样的问题。我刚刚将 assets.index 文件添加到我项目的资产文件夹中。该文件包含以下内容:

<?xml version="1.0"?>
<project default="createAssetIndex">
    <target name="createAssetIndex">
        <fileset id="assets.fileset" dir="assets/" includes="**"
            excludes="asset.index" />
        <pathconvert pathsep="${line.separator}" property="assets"
            refid="assets.fileset">
            <mapper>
                <globmapper from="${basedir}/assets/*" to="*"
                    handledirsep="yes" />
            </mapper>
        </pathconvert>
        <echo file="assets/assets.index">${assets}</echo>
    </target>
</project>

更多详情请关注link:determine wheteher android asset entry is a file or directory

demo 要求您将此代码块添加到项目的 build.gardle 文件中,以便在编译期间创建 assest.index 文件:

task indexAssets {
    description 'Index Build Variant assets for faster lookup by AssetManager'

    ext.assetsSrcDir = file( "${projectDir}/src/main/assets" )

    inputs.dir assetsSrcDir

    doLast {
        android.applicationVariants.each { target ->
            // create index
            def contents = ""
            def tree = fileTree(dir: "${ext.assetsSrcDir}", include: ['**/*.ttf'], exclude: ['**/.svn/**', '*.index'])
            // use this instead if you have assets folders in each flavor:
            // def tree = fileTree(dir: "${ext.variantPath}", exclude: ['**/.svn/**', '*.index'])
            tree.visit { fileDetails ->
                contents += "${fileDetails.relativePath}" + "\n"
            }

            // create index file
            def assetIndexFile = new File("${ext.assetsSrcDir}/assets.index")
            assetIndexFile.write contents
        }
    }
}

indexAssets.dependsOn {
    tasks.matching { task -> task.name.startsWith( 'merge' ) && task.name.endsWith( 'Assets' ) }
}

tasks.withType( JavaCompile ) {
    compileTask -> compileTask.dependsOn indexAssets
}

indexAssets

如果您没有资产文件夹,请在 Android Studio 中按以下方式创建它:

Select your project, then FILE --> NEW--> FOLDER --> ASSET

我发现如果资产中有 font folder,编辑器的启动速度比没有时更快。