我正在尝试使用android应用中的方法Color.luminance
更改颜色亮度,但我不断收到错误消息:
java.lang.NoSuchMethodError:类Landroid / graphics / Color中没有静态方法亮度(I)F;或其上级类(“ android.graphics.Color”的声明出现在/system/framework/framework.jar中)
在转到此静态方法的声明文档时,我看到:
/**
* Returns the relative luminance of a color.
* <p>
* Assumes sRGB encoding. Based on the formula for relative luminance
* defined in WCAG 2.0, W3C Recommendation 11 December 2008.
*
* @return a value between 0 (darkest black) and 1 (lightest white)
*/
public static float luminance(@ColorInt int color) {
ColorSpace.Rgb cs = (ColorSpace.Rgb) ColorSpace.get(ColorSpace.Named.SRGB);
DoubleUnaryOperator eotf = cs.getEotf();
double r = eotf.applyAsDouble(red(color) / 255.0);
double g = eotf.applyAsDouble(green(color) / 255.0);
double b = eotf.applyAsDouble(blue(color) / 255.0);
return (float) ((0.2126 * r) + (0.7152 * g) + (0.0722 * b));
}
很明显它在那里,但是在运行时它出错了,我的代码我不明白是怎么回事:
private float luminance = Color.luminance(color);
等级依赖性:
apply plugin: 'com.android.application'
android {
publishNonDefault true
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.george.value"
minSdkVersion 22
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:preference-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
testImplementation 'junit:junit:4.12'
}
答案 0 :(得分:1)
luminance (int color)
,在API 26中添加了luminance (long color)
,但是您的最小SDK为22
因为您的sdk编译为27,代码已成功编译,但是在具有android 6或更低版本的设备上会出现此异常,因此请将您的sdk的最低版本更改为24或找到其他解决方案
要更改最小sdk,您可以使用ColorUtils.calculateLuminance(int color);
中的android.support.v4.graphics.ColorUtils