在上篇文章Android性能优化第(八)篇---App启动速度优化之耗时检测处理说了应用启动的拦路虎在哪里,最后给出了优化思路,如果经过第一轮的优化之后,还是不满意的话,一般我们是做个启动页,因为启动页一般View的数量比较少,业务逻辑比较简单,启动就不太费劲。
一、设计思路
- 常规启动页设计思路
通常点击桌面就会执行,Application中的逻辑,这部分的逻辑优化技巧在上篇介绍过,然后会跳入启动页SplashActivity,通常展示一个图片和logo,然后2或3秒后,并且SplashActivity里面可以去做一些MainActivity的数据的预加载,然后需要通过意图传到MainActivity。
优点:启动速度有所加快
缺点:最终还是要进入首页,在进入首页的时候,首页复杂的View渲染以及必须在UI线程执行的业务逻辑,仍然拖慢了启动速度。启动页简单执行快,首页复杂执行慢,前轻后重。
期待:能否在启动页的展示的同时,首页的View就能够被加载,首页的业务逻辑就能够被执行?
- 优化的启动页的设计思路
为了在展示启动页的同时,也能渲染首页,把SplashActivity改成SplashFragment,应用程序的入口仍然是MainActivity,在MainActivity中先展示SplashFragment,当SplashFragment显示完毕后再将它remove,同时在SplashFragment的2S的友好时间内进行网络数据缓存,在窗口加载完毕后,我们加载activity_main的布局,考虑到这个布局有可能比较复杂,耽误View的解析时间,采用ViewStub的形式进行懒加载。这样一开始只要加载SplashFragment所展示的布局就Ok了。
二、实现
MainActivity .java
public class MainActivity extends FragmentActivity {
private MyHandler mHandler=new MyHandler(this);
public static final String TAG="MainActivity";
private ProgressBar mNetLoadingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
setContentView(R.layout.activity_main);
final SplashFragment splashFragment = new SplashFragment();
final ViewStub mainLayout = (ViewStub) findViewById(R.id.content_viewstub);
//1、一上来首先显示启动页面
FragmentManager supportFragmentManager = getSupportFragmentManager();
if (supportFragmentManager != null) {
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
if (fragmentTransaction != null) {
fragmentTransaction.replace(R.id.container, splashFragment);
fragmentTransaction.commit();
}
}
//2、如果主页有网络等耗时操作,可以现在就开始
new Thread(new Runnable() {
@Override
public void run() {
//耗时3500
SystemClock.sleep(3000);
mHandler.sendEmptyMessage(0);
}
}).start();
//3、渲染完毕后,立刻加载主页布局
getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
Log.d(TAG," getWindow().getDecorView().post");
View mainView = mainLayout.inflate();
initView(mainView);
}
});
//4、 启动页有动画,延迟一下,播放完动画,执行remove
getWindow().getDecorView().post(new Runnable() {
@Override
public void run() {
mHandler.postDelayed(new DelayRunnableImpl(MainActivity.this, splashFragment), 2000);
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
/**
* 初始化主页View
*/
private void initView(View pMainView) {
if (pMainView != null) {
mNetLoadingBar = (ProgressBar) pMainView.findViewById(R.id.progressbar);
mNetLoadingBar.setVisibility(View.VISIBLE);
}
}
private static class MyHandler extends Handler {
private WeakReference wRef;
private MyHandler(MainActivity pActivity) {
this.wRef = new WeakReference(pActivity);
}
@Override
public void handleMessage(Message msg) {
MainActivity mainActivity = wRef.get();
if (mainActivity != null) {
mainActivity.mNetLoadingBar.setVisibility(View.GONE);
}
}
}
private class DelayRunnableImpl implements Runnable {
WeakReference contextWref;
WeakReference fragmentWref;
private DelayRunnableImpl(Context pContext, Fragment pFragment) {
this.contextWref = new WeakReference<>(pContext);
this.fragmentWref = new WeakReference<>(pFragment);
}
@Override
public void run() {
FragmentActivity context = (FragmentActivity) contextWref.get();
if (context != null) {
FragmentManager supportFragmentManager = context.getSupportFragmentManager();
if (supportFragmentManager != null) {
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
SplashFragment fragment = (SplashFragment) fragmentWref.get();
if (fragment != null) {
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
}
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
}
}
SplashFragment .java
public class SplashFragment extends Fragment {
public SplashFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View ret = inflater.inflate(R.layout.fragment_splash, container, false);
initView(ret);
return ret;
}
private void initView(View ret) {
if (ret != null) {
ImageView imageView = (ImageView) ret.findViewById(R.id.laucher_logo);
playAnimator(imageView);
}
}
private void playAnimator(ImageView pView) {
if (pView != null) {
PropertyValuesHolder pvhA = PropertyValuesHolder.ofFloat("alpha", 1f, 0.7f, 0.1f);
// PropertyValuesHolder pvhR= PropertyValuesHolder.ofFloat("rotationX", 0.0F, 360.0F);
ObjectAnimator.ofPropertyValuesHolder(pView, pvhA).setDuration(2000).start();
}
}
}
activity_mian.xml
fragment.xml
main_layout.xml
效果怎么样呢?
看一下启动时间
C:\Users\wangjing>adb shell am start -W zhangwan.wj.com.myshare/zhangwan.wj.com.myshare.activity.MainActivity
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=zhangwan.wj.com.myshare/.activity.MainActivity }
Status: ok
Activity: zhangwan.wj.com.myshare/.activity.MainActivity
ThisTime: 683
TotalTime: 683
Complete
还是挺不错的吧。
参考链接:请不要滥用SharedPreference https://zhuanlan.zhihu.com/p/22913991
Please accept mybest wishes for your happiness and success !