我试图在我的安卓应用程序中使用Color.luminance的方法来改变颜色亮度,但我一直在犯错误:
java.lang.NoSuchMethodError:没有静态方法亮度(I)F的类Landroid/图形/颜色;或它的超类( 'android.graphics.Color‘的声明出现在/system/F/Frawork.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'
}发布于 2018-07-29 19:21:33
luminance (int color)是在api 24中添加的,luminance (long color)是在api 26中添加的,但是min是22。
因为您的编译sdk是27,所以成功编译了代码,但是在使用android 6的设备上和更低的设备上,您会得到此异常,所以将min版本更改为24或找到另一种解决方案。
在更改min时,可以使用来自android.support.v4.graphics.ColorUtils的android.support.v4.graphics.ColorUtils
https://stackoverflow.com/questions/51583596
复制相似问题