本人原創(chuàng)鏈接:https://ask.dcloud.net.cn/article/39413
這幾天開始接觸uniapp開發(fā),發(fā)現(xiàn)官方demo安卓端點(diǎn)擊app圖標(biāo)到顯示啟動(dòng)頁(Hbuildx圖標(biāo)頁)會(huì)卡個(gè)1-2秒
明顯比原生app體驗(yàn)差很多关翎,作為一個(gè)強(qiáng)迫癥扛门,這點(diǎn)當(dāng)然是不能忍的。具體問題詳情查看以下鏈接:
https://ask.dcloud.net.cn/question/133649?notification_id-987295rf-falseitem_id-177937#!answer_177937
先說下大概為什么卡頓的原因纵寝,我也不太明白论寨,我猜是因?yàn)閱?dòng)安卓app時(shí)需要啟動(dòng)webview或者weex等底層引擎導(dǎo)致的卡頓。(這個(gè)問題望官方的人可以解答一下,我看了PandoraEntry的源碼也沒有找到答案)
下面說一下優(yōu)化原理葬凳,就是用原生安卓代碼先啟動(dòng)一個(gè)頁面贞铣,然后在該頁面中再啟動(dòng)uni的主activity(PandoraEntry)
這樣,點(diǎn)擊app圖標(biāo)后沮明,就跟原生一樣會(huì)立馬顯示我們?cè)_發(fā)的第一個(gè)頁面(而不會(huì)卡個(gè)1辕坝、2秒才顯示界面,讓人感覺以為自己沒點(diǎn)擊圖標(biāo)成功)
下面說下流程
一荐健、在AS中新建一個(gè)empty activity:菜單file->new->activity->empty activity酱畅。
界面xml代碼:
```
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"?
? ? xmlns:app="http://schemas.android.com/apk/res-auto"?
? ? xmlns:tools="http://schemas.android.com/tools"?
? ? android:layout_width="match_parent"?
? ? android:layout_height="match_parent"?
? ? tools:context="com.xiefeng.MainActivity">?
</androidx.constraintlayout.widget.ConstraintLayout>
```
沒什么東西,就是一個(gè)空白頁面
java代碼如下:
package com.xiefeng;
import androidx.appcompat.app.AppCompatActivity;?
import android.content.Intent;?
import android.os.Bundle;?
import com.android.simple.R;?
import io.dcloud.PandoraEntry;?
public class MainActivity extends AppCompatActivity {?
? ? @Override?
? ? protected void onCreate(Bundle savedInstanceState) {?
? ? ? ? super.onCreate(savedInstanceState);?
? ? ? ? setContentView(R.layout.activity_main);?
? ? ? ? Thread myThread=new Thread(){//創(chuàng)建子線程江场,啟動(dòng)uni的主activity?
? ? ? ? ? ? @Override?
? ? ? ? ? ? public void run() {?
? ? ? ? ? ? ? ? try{?
? ? ? ? ? ? ? ? ? ? sleep(1000);//使程序休眠五秒?
? ? ? ? ? ? ? ? ? ? Intent it=new Intent(MainActivity.this, PandoraEntry.class);//啟動(dòng)MainActivity?
? ? ? ? ? ? ? ? ? ? startActivity(it);?
? ? ? ? ? ? ? ? ? ? finish();//關(guān)閉當(dāng)前活動(dòng)?
? ? ? ? ? ? ? ? }catch (Exception e){?
? ? ? ? ? ? ? ? ? ? e.printStackTrace();?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }?
? ? ? ? };?
? ? ? ? myThread.start();//啟動(dòng)線程?
? ? }?
}
二纺酸、AndroidManifest.xml修改:
1、在android節(jié)點(diǎn)加上主題android:theme="@style/AppTheme2"
2址否、新增我們自定義的activity并設(shè)為main
3餐蔬、將uni的主activity的main注釋掉
完整代碼如下:
??? ? ? ? ? ? ? ? ? ? <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"?
? ? package="com.android.simple">?
? ? <application?
? ? ? ? android:allowBackup="true"?
? ? ? ? android:allowClearUserData="true"?
? ? ? ? android:icon="@drawable/icon"?
? ? ? ? android:label="@string/app_name"?
? ? ? ? android:largeHeap="true"?
? ? ? ? android:supportsRtl="true"?
? ? ? ? android:theme="@style/AppTheme2">?
? ? ? ? <activity android:name="com.xiefeng.MainActivity">?
? ? ? ? ? ? <intent-filter>?
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />?
? ? ? ? ? ? </intent-filter>?
? ? ? ? </activity>?
? ? ? ? <activity?
? ? ? ? ? ? android:name="io.dcloud.PandoraEntry"?
? ? ? ? ? ? android:configChanges="orientation|keyboardHidden|keyboard|navigation"?
? ? ? ? ? ? android:hardwareAccelerated="true"?
? ? ? ? ? ? android:label="@string/app_name"?
? ? ? ? ? ? android:launchMode="singleTask"?
? ? ? ? ? ? android:screenOrientation="user"?
? ? ? ? ? ? android:theme="@style/TranslucentTheme"?
? ? ? ? ? ? android:windowSoftInputMode="adjustResize">?
? ? ? ? ? ? <!--<intent-filter>?
? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />?
? ? ? ? ? ? </intent-filter>?
? ? ? ? ? ? -->?
? ? ? ? </activity>?
? ? ? ? <activity?
? ? ? ? ? ? android:name="io.dcloud.PandoraEntryActivity"?
? ? ? ? ? ? android:configChanges="orientation|keyboardHidden|screenSize|mcc|mnc|fontScale|keyboard|smallestScreenSize|screenLayout|screenSize"?
? ? ? ? ? ? android:hardwareAccelerated="true"?
? ? ? ? ? ? android:launchMode="singleTask"?
? ? ? ? ? ? android:permission="com.miui.securitycenter.permission.AppPermissionsEditor"?
? ? ? ? ? ? android:screenOrientation="user"?
? ? ? ? ? ? android:theme="@style/DCloudTheme"?
? ? ? ? ? ? android:windowSoftInputMode="adjustResize">?
? ? ? ? ? ? <intent-filter>?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.DEFAULT" />?
? ? ? ? ? ? ? ? <category android:name="android.intent.category.BROWSABLE" />?
? ? ? ? ? ? ? ? <action android:name="android.intent.action.VIEW" />?
? ? ? ? ? ? ? ? <data android:scheme="h56131bcf" />?
? ? ? ? ? ? </intent-filter>?
? ? ? ? </activity>?
? ? ? ? <provider?
? ? ? ? ? ? android:name="io.dcloud.common.util.DCloud_FileProvider"?
? ? ? ? ? ? android:authorities="com.android.simple.dc.fileprovider"?
? ? ? ? ? ? android:exported="false"?
? ? ? ? ? ? android:grantUriPermissions="true">?
? ? ? ? ? ? <meta-data?
? ? ? ? ? ? ? ? android:name="android.support.FILE_PROVIDER_PATHS"?
? ? ? ? ? ? ? ? android:resource="@xml/dcloud_file_provider" />?
? ? ? ? </provider>?
? ? ? ? <meta-data?
? ? ? ? ? ? android:name="dcloud_appkey"?
? ? ? ? ? ? android:value="69ae588821d34380d31b94dd33dc0689" />?
? ? </application>?
</manifest>
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -->? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
三、最后在values/styles.xml加上主題:
<style name="AppTheme2" parent="Theme.AppCompat.Light.NoActionBar">
? ? ? ? <item name="android:windowFullscreen">true</item>?
? ? ? ? <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>?
? ? ? ? <item name="android:windowDrawsSystemBarBackgrounds">false</item>?
? ? ? ? <item name="colorPrimary">#6200EE</item>?
? ? ? ? <item name="colorPrimaryDark">#6200EE</item>?
? ? ? ? <item name="colorAccent">#6200EE</item>?
? ? </style>
如果要加上自己的啟動(dòng)圖片佑附,可以加上節(jié)點(diǎn):<item name="android:windowBackground">@drawable/splash</item>樊诺,并把圖片放到drawable文件夾下。
建議原生啟動(dòng)圖和Hbuildx設(shè)置的啟動(dòng)圖為同一張音同,這樣兩張啟動(dòng)圖無縫對(duì)接词爬,體驗(yàn)比較好。
(注:加上啟動(dòng)圖后啟動(dòng)會(huì)更慢個(gè)0.1秒不知道為什么权均,希望大神們解答一下)
完畢顿膨。