Kursad
Kursad

Reputation: 113

What is the impact of setVisibility changing Visible to Visible? Android

Sometimes I need to change my image's visibility from the java code and I don't want to check every time whether the status of visibility is equal of the value of I want to change to. Namely, an image is visible and I want to change into visible again. What is the cost of that function or is there any cost of that function?

Upvotes: 1

Views: 170

Answers (1)

Yavor Mitev
Yavor Mitev

Reputation: 1508

There is a check at the beginning of the method part of the View class so I would not bother to check myself:

/**
     * Set the visibility state of this view.
     *
     * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
     * @attr ref android.R.styleable#View_visibility
     */
    @RemotableViewMethod
    public void setVisibility(@Visibility int visibility) {
        setFlags(visibility, VISIBILITY_MASK);
    }

 /**
     * Set flags controlling behavior of this view.
     *
     * @param flags Constant indicating the value which should be set
     * @param mask Constant indicating the bit range that should be changed
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    void setFlags(int flags, int mask) {
        final boolean accessibilityEnabled =
                AccessibilityManager.getInstance(mContext).isEnabled();
        final boolean oldIncludeForAccessibility = accessibilityEnabled && includeForAccessibility();

        int old = mViewFlags;
        mViewFlags = (mViewFlags & ~mask) | (flags & mask);

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

Upvotes: 3

Related Questions