代码之家  ›  专栏  ›  技术社区  ›  George Udosen

我无法访问颜色。亮度静态方法

  •  1
  • George Udosen  · 技术社区  · 8 年前

    我正试图用这种方法改变颜色亮度 Color.luminance 在我的android应用程序中,但我不断收到错误:

    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'
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Amani    8 年前

    luminance (int color) 在api 24和 luminance (long color) 在api 26中,但您的min sdk是22

    因为您的compile sdk是27,代码编译成功,但在android 6及更低版本的设备上,您会遇到此异常,所以请将min sdk版本更改为24,或找到其他解决方案

    更改可以使用的最小sdk的说明 ColorUtils.calculateLuminance(int color); android.support.v4.graphics.ColorUtils