Android 视图:在设置可见性之前检查可见性可以提高性能吗? [英] Android Views: can checking visibility before setting visibility improve performance?

查看:54
本文介绍了Android 视图:在设置可见性之前检查可见性可以提高性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些辅助方法可以根据传递给方法的状态变量设置某些 View 的可见性.有时,这些方法会被多次调用并且 View 的可见性不会改变.所以我发现自己在设置之前开始检查每个 View 的可见性,将视图的可见性更改为相同的可见性并无缘无故地导致刷新毫无意义".

I have helper methods that set the visibility of certain Views depending on the state variables that are passed into the methods. Sometimes, these methods will get called many times and the Views visibility will not change. So I found myself starting to check the visibility of each View before setting it with the thinking, "No point in changing a View's visibility to the same visibility and causing a refresh for no reason".

            if (myView.getVisibility() != View.VISIBLE) {
                myView.setVisibility(View.VISIBLE);
            }
            etc...

但是,现在我想知道 setVisibility 的实现是否已经考虑到这一点,并检查您是否为 View 已经拥有的设置了相同的可见性,并且不会不必要地刷新 View(我的代码正在尝试做什么).

However, now I'm wondering if the implementation of setVisibility already takes this into account, and checks if you are setting the same visibility to what the View already has, and doesn't needlessly refresh the View (what my code is trying to do).

那么有谁知道我的优化"是否真的提高了任何性能,或者 API 是否已经领先我一步?

So does anyone know if my "optimization" is actually improving any performance, or is the API already a step ahead of me?

推荐答案

他们已经领先一步了.查看View.java:setVisibility()的代码:

They're already one step ahead. See the code for View.java:setVisibility():

public void setVisibility(int visibility) {
    setFlags(visibility, VISIBILITY_MASK);
    ...
}

它调用setFlags():

void setFlags(int flags, int mask) {
    int old = mViewFlags;
    mViewFlags = (mViewFlags & ~mask) | (flags & mask);

    int changed = mViewFlags ^ old;
    if (changed == 0) {
        return;
    }
    ....
} 

它检查标志是否与当前状态匹配.如果是这样,它只会返回而不做任何事情.

It checks to see if the flag matches the current state. If so, it simply returns without doing anything.

这篇关于Android 视图:在设置可见性之前检查可见性可以提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆