国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Adroid性能優化--啟動頁優化

2019-11-09 19:07:02
字體:
來源:轉載
供稿:網友

一般情況下,app在首次啟動時需要加載大量布局跟數據,我公司項目首次啟動需要加載一個Activity里面有7個Fragment…加上各種數據,經常導致啟動時會有白屏現象,為了完美解決各種現象也花了一些功夫,這里記錄一下. 1:點擊App啟動時第一個白屏 這個白屏是因為主題背景設置導致的,解決也比較簡單,就是設置App主題背景圖片為自己App 加載的圖片即可,這里注意要把加載Activity跟布局的背景去掉,不然主題的背景會被覆蓋!

<!-- application theme. --><style name="A然后在清單文件設置主題就可以!

2:splash 界面過后,跳轉主界面時間過長,出現白屏; 這種情況就是主界面布局復雜,加載時間長導致的或者主界面加載過程有耗時操作;這里不討論耗時操作的影響,有點經驗都不會放到主線程; 復雜的布局改怎么處理?總不能不加載吧… 這里使用延時加載子布局方法 以前我們的思路是先加載splash界面,一定時間后再跳轉到主界面,這個思路有2個缺點 第一: 加載splash界面時不能加載網絡數據(加載了也要做緩存,因為這個界面很快就要被回收),浪費了中間的寶貴時間 第二:加載splash界面時不能預加載復雜的主界面,導致跳轉到主界面后卡頓,白屏! 這里的思路是將 splash界面圖片直接放在主界面,用延時加載布局方法先加載圖片后再加載復雜布局; 下面上代碼

import
java.lang.ref.WeakReference;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTransaction;import android.view.ViewStub;public class MainActivity extends FragmentActivity { PRivate Handler mHandler = new Handler(); private SplashFragment splashFragment; private ViewStub viewStub; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); splashFragment = new SplashFragment(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame, splashFragment); transaction.commit();// mHandler.postDelayed(new Runnable() {// @Override// public void run() {// mProgressBar.setVisibility(View.GONE);// iv.setVisibility(View.VISIBLE);// }// }, 2500); viewStub = (ViewStub)findViewById(R.id.content_viewstub); //1.判斷當窗體加載完畢的時候,立馬再加載真正的布局進來 getWindow().getDecorView().post(new Runnable() { @Override public void run() { // 開啟延遲加載 mHandler.post(new Runnable() { @Override public void run() { //將viewstub加載進來 viewStub.inflate(); } } ); } }); //2.判斷當窗體加載完畢的時候執行,延遲一段時間做動畫。 getWindow().getDecorView().post(new Runnable() { @Override public void run() { // 開啟延遲加載,也可以不用延遲可以立馬執行(我這里延遲是為了實現fragment里面的動畫效果的耗時) mHandler.postDelayed(new DelayRunnable(MainActivity.this, splashFragment) ,2000);// mHandler.post(new DelayRunnable()); } }); //3.同時進行異步加載數據 } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } static class DelayRunnable implements Runnable{ private WeakReference<Context> contextRef; private WeakReference<SplashFragment> fragmentRef; public DelayRunnable(Context context, SplashFragment f) { contextRef = new WeakReference<Context>(context); fragmentRef = new WeakReference<SplashFragment>(f); } @Override public void run() { // 移除fragment if(contextRef!=null){ SplashFragment splashFragment = fragmentRef.get(); if(splashFragment==null){ return; } FragmentActivity activity = (FragmentActivity) contextRef.get(); FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction(); transaction.remove(splashFragment); transaction.commit(); } } }}

簡單的Fragmemt

import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class SplashFragment extends Fragment { @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_splash, container,false); }}

布局activity_main

<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" tools:context="com.example.applicationstartoptimizedemo.MainActivity" > <ViewStub android:id="@+id/content_viewstub" android:layout="@layout/activity_main_viewstub" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout></RelativeLayout>

activity_main_viewstub

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" > <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitStart" android:src="@drawable/content" /></FrameLayout>

fragment_splash

<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" tools:context="com.example.applicationstartoptimizedemo.MainActivity" > <FrameLayout android:id="@+id/frame" android:background="@drawable/splash12" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout></RelativeLayout>

主要就是用到 ViewStub 延時加載就機制.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 红桥区| 龙口市| 乌鲁木齐市| 桓仁| 彩票| 巴南区| 德清县| 江门市| 武安市| 锦屏县| 阿瓦提县| 门头沟区| 绥滨县| 石狮市| 宜宾市| 会泽县| 凤凰县| 无极县| 漯河市| 南皮县| 韩城市| 吉安县| 芦山县| 五峰| 西盟| 于田县| 竹北市| 定襄县| 四平市| 永新县| 桐庐县| 泗洪县| 报价| 怀仁县| 眉山市| 肥东县| 台东市| 莱州市| 修武县| 新竹市| 军事|