Android应用开发那些事android开发技巧优化

Android布局优化(四)X2C — 提升布局加载速度200%

2019-12-19  本文已影响0人  Geekholt

系列文章

前言

Android布局优化(一)从布局加载原理说起中我们说到了布局加载的两大性能瓶颈,通过IO操作将XML加载到内存中并进行解析和通过反射创建View。这里介绍一种避免运行时通过IO操作读取布局文件的“黑科技”—X2C。个人认为这个技术在实际项目中使用可能会需要处理一些兼容性问题,但是从技术人员的思维发散的角度,我认为这其中的一些思想还是很值得学习和借鉴的

以下内容转载自掌阅X2C官方github文档

背景

一般大家在写页面时都是通过xml写布局,通过setContentView、或LayoutInflater.from(context).inflate方法将xml布局加载到内存中

优点

缺点

我们团队在这个问题上也探索过很多解决方案,一度走到了另一个极端,完全废弃xml,所有控件通过java来new,甚至直接在canvas里绘制,这样虽然性能确实提升了,但是代码已经没有了一丁点可读性,可维护性。  我们后来反思代码到底是给机器看的,还是给人看的??也许X2C已经给了我们一个答案

X2C

为了即保留xml的优点,又解决它带来的性能问题,我们开发了X2C方案。即在编译生成APK期间,将需要翻译的layout翻译生成对应的java文件,这样对于开发人员来说写布局还是写原来的xml,但对于程序来说,运行时加载的是对应的java文件。     我们采用APT(Annotation Processor Tool)+ JavaPoet技术来完成编译期间【注解】->【解注解】->【翻译xml】->【生成java】整个流程的操作。

性能对比

在开发集成完之后我们做了简单的测试,性能对比如下

加载方式 次数 平均加载时间
XML 100 30
X2C 100 11

集成使用

1.导入依赖

在module的build.gradle文件添加依赖

annotationProcessor 'com.zhangyue.we:x2c-apt:1.1.2'
implementation 'com.zhangyue.we:x2c-lib:1.0.6'

2.添加注解

在使用布局的任意java类或方法添加注解即可

@Xml(layouts = "activity_main")

3.配置自定义属性(没有可不配)

在module下建立X2C_CONFIG.xml文件,里面配置定义属性和方法的映射关系,如果接收者是view,则写view.否则填params.

<x2c-config>
    <attr name="app:mixColor" toFunc="view.setMixColor(int)" />
    <attr name="android:layout_marginTop" toFunc="params.topMargin=int" />
</x2c-config>

4.通过X2C加载布局

在原先使用setContentView或inflate的地方替换,如下:

this.setContentView(R.layout.activity_main); --> X2C.setContentView(this, R.layout.activity_main);
LayoutInflater.from(this).inflate(R.layout.activity_main,null); --> X2C.inflate(this,R.layout.activity_main,null);

过程文件

原始的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
  android:paddingLeft="10dp">

  <include
      android:id="@+id/head"
      layout="@layout/head"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true" />

  <ImageView
      android:id="@+id/ccc"
      style="@style/bb"
      android:layout_below="@id/head" />
</RelativeLayout>

生成的java文件

/**
 * WARN!!! dont edit this file
 * translate from {@link  com.zhangyue.we.x2c.demo.R.layout.activity_main}
 * autho chengwei
 * email chengwei@zhangyue.com
 */
public class X2C_2131296281_Activity_Main implements IViewCreator {
  @Override
  public View createView(Context ctx, int layoutId) {
        Resources res = ctx.getResources();

        RelativeLayout relativeLayout0 = new RelativeLayout(ctx);
        relativeLayout0.setPadding((int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,res.getDisplayMetrics())),0,0,0);

        View view1 =(View) new X2C_2131296283_Head().createView(ctx,0);
        RelativeLayout.LayoutParams layoutParam1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        view1.setLayoutParams(layoutParam1);
        relativeLayout0.addView(view1);
        view1.setId(R.id.head);
        layoutParam1.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);

        ImageView imageView2 = new ImageView(ctx);
        RelativeLayout.LayoutParams layoutParam2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,(int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,1,res.getDisplayMetrics())));
        imageView2.setLayoutParams(layoutParam2);
        relativeLayout0.addView(imageView2);
        imageView2.setId(R.id.ccc);
        layoutParam2.addRule(RelativeLayout.BELOW,R.id.head);

        return relativeLayout0;
  }
}

生成的映射文件

/**
 * WARN!!! don't edit this file
 *
 * author chengwei
 * email chengwei@zhangyue.com
 */
public class X2C127_activity implements IViewCreator {
  @Override
  public View createView(Context context) {
        View view = null ;
        int sdk = Build.VERSION.SDK_INT;
        int orientation = context.getResources().getConfiguration().orientation;
        boolean isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE;
        if (isLandscape) {
            view = new com.zhangyue.we.x2c.layouts.land.X2C127_Activity().createView(context);
        } else if (sdk >= 27) {
            view = new com.zhangyue.we.x2c.layouts.v27.X2C127_Activity().createView(context);
        } else if (sdk >= 21) {
            view = new com.zhangyue.we.x2c.layouts.v21.X2C127_Activity().createView(context);
        } else {
            view = new com.zhangyue.we.x2c.layouts.X2C127_Activity().createView(context);
        }
        return view;
  }
}

不支持

支持

属性名称 属性名称
android:textSize app:layout_constraintRight_toLeftOf
android:textColor app:layout_constraintBottom_toTopOf
android:text app:layout_constraintTop_toTopOf
android:background app:layout_constrainedHeight
查看全部
上一篇下一篇

猜你喜欢

热点阅读