gpt4 book ai didi

安卓颜色,最快?

转载 作者:行者123 更新时间:2023-11-29 15:12:48 29 4
gpt4 key购买 nike

什么是更快的安卓?

Color.rgb(184, 134, 011);

Color.parseColor("#234181");

还是别的什么?

答案:最快的似乎是:

int mycolor = 0xff234181;

感谢 samgak 和 KenWolf。

但是现在我想知道常用函数是如何处理它的,这里是 2 View setbackgroundcolor() 和 TextView settextcolor() 和(一些)以下函数的来源:

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);
}

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() 以 invalidate() 结束。

setBackgroundColor() 以 invalidate() 结束?或 .mutate()).setColor(color) ?或 applyBackgroundTint() ?

最佳答案

https://github.com/android/platform_frameworks_base/blob/master/graphics/java/android/graphics/Color.java

public static int rgb(int red, int green, int blue) {
return (0xFF << 24) | (red << 16) | (green << 8) | blue;
}

对比

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");
}

我没有测量过,但我猜

Color.rgb(184, 134, 011));

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

我认为在实践中这种差异最多可以忽略不计。

关于安卓颜色,最快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28756078/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com