android 4.2 rtlSupport对UI性能的坑

最近在android 4.2上发现APP特别卡顿的问题,通过抓trace发现很多耗时在ViewGroup.resolveRtlPropertiesIfNeeded, View.resolveRtlPropertiesIfNeeded方法

查看相关代码,google4.2开始支持rtl,也就是右向坐right to left布局(一些很稀有的小语种国家)

发现每次重新mearsure都要对布局进行resolveRtlPropertiesIfNeeded

通过查看代码,发现4.2上这个功能做得不完善,4.2的代码:

    /**
     * @hide
     */
    @Override
    public void resolveRtlPropertiesIfNeeded() {
        super.resolveRtlPropertiesIfNeeded();
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.isLayoutDirectionInherited()) {
                child.resolveRtlPropertiesIfNeeded();
            }
        }
    }

递归调用时并没有判断是否需要对子view进行递归resolveRtlPropertiesIfNeeded。 并且View的默认rtlSupport是inherit,导致每次measure导游重新递归resolveRtlPropertiesIfNeeded

再看看4.3的代码:

    /**
     * @hide
     */
    @Override
    public boolean resolveRtlPropertiesIfNeeded() {
        final boolean result = super.resolveRtlPropertiesIfNeeded();
        // We dont need to resolve the children RTL properties if nothing has changed for the parent
        if (result) {
            int count = getChildCount();
            for (int i = 0; i < count; i++) {
                final View child = getChildAt(i);
                if (child.isLayoutDirectionInherited()) {
                    child.resolveRtlPropertiesIfNeeded();
                }
            }
        }
        return result;
    }

由于没找到4,3的手机实际测试,没法确定4.3上android是否彻底解决这个问题,但是在5.0以上手机确实没发现一次measure过程resolveRtlPropertiesIfNeeded递归次数特别多。

针对这种情况,如果APP不需要支持右向左的小语种,可以在AndroidMenifest.xml声明关闭rtlSupport,并在style中声明layoutDirection为ltr

<application
    android:supportsRtl="false"

<style name="xxxx" parent="@android:style/Theme.Light.NoTitleBar">
    <item name="android:layoutDirection" tools:targetApi="jelly_bean_mr1">ltr</item>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值