首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android颜色最快?

Android颜色最快?
EN

Stack Overflow用户
提问于 2015-02-27 01:38:02
回答 3查看 900关注 0票数 1

什么是更快的android?

代码语言:javascript
复制
Color.rgb(184, 134, 011);

代码语言:javascript
复制
Color.parseColor("#234181");

还是别的什么?

的回答:最快的似乎是:

代码语言:javascript
复制
int mycolor = 0xff234181;

多亏了samgak和KenWolf。

然而,现在我想知道常见的函数是如何处理它的,下面是2 Viewsetback背景颜色()和TextView settextcolor()和(一些)的源代码

代码语言:javascript
复制
public void setBackgroundColor(int color) {
    if (mBackground instanceof ColorDrawable) {
        ((ColorDrawable) mBackground.mutate()).setColor(color);
        computeOpaqueFlags();
        mBackgroundResource = 0;
    } else {
        setBackground(new ColorDrawable(color));
    }
}

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

public void setBackgroundDrawable(Drawable background) {
    computeOpaqueFlags();

    if (background == mBackground) {
        return;
    }

    boolean requestLayout = false;

    mBackgroundResource = 0;

    /*
     * Regardless of whether we're setting a new background or not, we want
     * to clear the previous drawable.
     */
    if (mBackground != null) {
        mBackground.setCallback(null);
        unscheduleDrawable(mBackground);
    }

    if (background != null) {
        Rect padding = sThreadLocal.get();
        if (padding == null) {
            padding = new Rect();
            sThreadLocal.set(padding);
        }
        resetResolvedDrawables();
        background.setLayoutDirection(getLayoutDirection());
        if (background.getPadding(padding)) {
            resetResolvedPadding();
            switch (background.getLayoutDirection()) {
                case LAYOUT_DIRECTION_RTL:
                    mUserPaddingLeftInitial = padding.right;
                    mUserPaddingRightInitial = padding.left;
                    internalSetPadding(padding.right, padding.top, padding.left, padding.bottom);
                    break;
                case LAYOUT_DIRECTION_LTR:
                default:
                    mUserPaddingLeftInitial = padding.left;
                    mUserPaddingRightInitial = padding.right;
                    internalSetPadding(padding.left, padding.top, padding.right, padding.bottom);
            }
            mLeftPaddingDefined = false;
            mRightPaddingDefined = false;
        }

        // Compare the minimum sizes of the old Drawable and the new.  If there isn't an old or
        // if it has a different minimum size, we should layout again
        if (mBackground == null
                || mBackground.getMinimumHeight() != background.getMinimumHeight()
                || mBackground.getMinimumWidth() != background.getMinimumWidth()) {
            requestLayout = true;
        }

        background.setCallback(this);
        if (background.isStateful()) {
            background.setState(getDrawableState());
        }
        background.setVisible(getVisibility() == VISIBLE, false);
        mBackground = background;

        applyBackgroundTint();

        if ((mPrivateFlags & PFLAG_SKIP_DRAW) != 0) {
            mPrivateFlags &= ~PFLAG_SKIP_DRAW;
            mPrivateFlags |= PFLAG_ONLY_DRAWS_BACKGROUND;
            requestLayout = true;
        }
    } else {
        /* Remove the background */
        mBackground = null;

        if ((mPrivateFlags & PFLAG_ONLY_DRAWS_BACKGROUND) != 0) {
            /*
             * This view ONLY drew the background before and we're removing
             * the background, so now it won't draw anything
             * (hence we SKIP_DRAW)
             */
            mPrivateFlags &= ~PFLAG_ONLY_DRAWS_BACKGROUND;
            mPrivateFlags |= PFLAG_SKIP_DRAW;
        }

        /*
         * When the background is set, we try to apply its padding to this
         * View. When the background is removed, we don't touch this View's
         * padding. This is noted in the Javadocs. Hence, we don't need to
         * requestLayout(), the invalidate() below is sufficient.
         */

        // The old background's minimum size could have affected this
        // View's layout, so let's requestLayout
        requestLayout = true;
    }

    computeOpaqueFlags();

    if (requestLayout) {
        requestLayout();
    }

    mBackgroundSizeChanged = true;
    invalidate(true);
}

代码语言:javascript
复制
public void setTextColor(int color) {
mTextColor = ColorStateList.valueOf(color);
    updateTextColors();
}

public void setTextColor(ColorStateList colors) {
    if (colors == null) {
        throw new NullPointerException();
    }

    mTextColor = colors;
    updateTextColors();
}

private void updateTextColors() {
    boolean inval = false;
    int color = mTextColor.getColorForState(getDrawableState(), 0);
    if (color != mCurTextColor) {
        mCurTextColor = color;
        inval = true;
    }
    if (mLinkTextColor != null) {
        color = mLinkTextColor.getColorForState(getDrawableState(), 0);
        if (color != mTextPaint.linkColor) {
            mTextPaint.linkColor = color;
            inval = true;
        }
    }
    if (mHintTextColor != null) {
        color = mHintTextColor.getColorForState(getDrawableState(), 0);
        if (color != mCurHintTextColor && mText.length() == 0) {
            mCurHintTextColor = color;
            inval = true;
        }
    }
    if (inval) {
        // Text needs to be redrawn with the new color
        if (mEditor != null) mEditor.invalidateTextDisplayList();
        invalidate();
    }
}

setTextColor()以无效()结尾。

setBackgroundColor()以无效()结尾?还是.mutate()).setColor(颜色)?还是applyBackgroundTint()?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-02-27 02:15:08

还是别的什么?

这取决于你经过的是什么。如果要将硬编码值传递到函数中,并将返回值分配给整数变量,则最快的方法是根本不调用任何函数,而是直接将表示颜色的十六进制值赋值给整数变量。

例如:

代码语言:javascript
复制
int colorValue = Color.rgb(184, 134, 011);

可以用

代码语言:javascript
复制
int colorValue = 0xffb8860b;

这就是无论如何从Color.rgb()返回的内容。

同样的

代码语言:javascript
复制
int colorValue = Color.parseColor("#234181");

可以用

代码语言:javascript
复制
int colorValue = 0xff234181;

这牺牲了可读性和便利性,以提高速度(可能可以忽略不计),但您可以通过将十六进制颜色值声明为具有意义的颜色名称的static final int常量来提高其可读性。

票数 2
EN

Stack Overflow用户

发布于 2015-02-27 01:48:05

base/blob/master/graphics/java/android/graphics/Color.java

代码语言:javascript
复制
public static int rgb(int red, int green, int blue) {
    return (0xFF << 24) | (red << 16) | (green << 8) | blue;
}

vs

代码语言:javascript
复制
public static int parseColor(String colorString) {
    if (colorString.charAt(0) == '#') {
        // Use a long to avoid rollovers on #ffXXXXXX
        long color = Long.parseLong(colorString.substring(1), 16);
        if (colorString.length() == 7) {
            // Set the alpha value
            color |= 0x00000000ff000000;
        } else if (colorString.length() != 9) {
            throw new IllegalArgumentException("Unknown color");
        }
        return (int)color;
    } else {
        Integer color = sColorNameMap.get(colorString.toLowerCase(Locale.ROOT));
        if (color != null) {
            return color;
        }
    }
    throw new IllegalArgumentException("Unknown color");
}

我还没量过,但我猜

代码语言:javascript
复制
Color.rgb(184, 134, 011));

由于它使用简单的位移位,并且必须处理较小的输入集,因此速度更快。

我可以想象,在实践中,这种差别充其量是微不足道的。

票数 3
EN

Stack Overflow用户

发布于 2015-02-27 01:52:37

当然,Color.rgb(184, 134, 011)Colors.parseColor()快。这是因为现在没有必要将hex转换为rgb,而您在提供rgb之前,但仍然低于推荐的方式在安卓中使用颜色-

在colors.xml -中添加以下内容

代码语言:javascript
复制
<color name="my_color">#234181 </color>

如果colors.xml在res/values中不存在,那么首先在res/values文件夹中创建它,然后添加上面的行。

然后你就可以通过做这样的事情来得到代码中的颜色-

在您的活动和片段-

代码语言:javascript
复制
getResources().getColor(R.color.my_color);

访问活动或应用程序的任何其他类中的Context - -

代码语言:javascript
复制
context.getResources().getColor(R.color.my_color);

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28756078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档