Android绘图的Paint.setBlendMode导致崩溃问题。

huangapple go评论145阅读模式
英文:

Android Paint.setBlendMode Crash

问题

我正在使用绘图工具paint在画布上绘制一些文本上方的突出显示框highlight boxes)。我需要将绘图工具的混合模式blendmode设置为multiply

```java
Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setBlendMode(BlendMode.MULTIPLY);

然而,这会导致崩溃:
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendMode;

在我的Oneplus 6T上不会发生这种情况,那里它可以正常工作。然而,在我的LG V10上会出现这种崩溃。

有关如何修复这个问题的任何想法吗?

build.gradle:

apply plugin: 'com.android.application';

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 24
        targetSdkVersion 29
        multiDexEnabled true
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.google.android.material:material:1.3.0-alpha01'

    implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
    implementation 'com.tom_roush:pdfbox-android:1.8.10.1'
    implementation files('libs\\YouTubeAndroidPlayerApi.jar')
}

<details>
<summary>英文:</summary>

I&#39;m using paint to draw highlight boxes on top of some text using a canvas. I need to set the paint blendmode to multiply. 

Paint paint = new Paint();
paint.setColor(Color.YELLOW);
paint.setBlendMode(BlendMode.MULTIPLY);


This however, causes a crash:
**java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendMode;**

It doesn&#39;t happen on my Oneplus 6T where it works perfectly. On my LG V10 however, this crash happens.

Any ideas on how to fix this?

**build.gradle:**

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
    applicationId &quot;com.example.test&quot;
    minSdkVersion 24
    targetSdkVersion 29
    multiDexEnabled true
    versionCode 1
    versionName &quot;1.0&quot;

    testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
    }
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
implementation &#39;androidx.legacy:legacy-support-v4:1.0.0&#39;
testImplementation &#39;junit:junit:4.12&#39;
androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;

implementation &#39;com.google.android.material:material:1.3.0-alpha01&#39;

implementation &#39;com.github.barteksc:android-pdf-viewer:3.2.0-beta.1&#39;
implementation &#39;com.tom_roush:pdfbox-android:1.8.10.1&#39;
implementation files(&#39;libs\\YouTubeAndroidPlayerApi.jar&#39;)

}



</details>


# 答案1
**得分**: 1

***已解决***

显然,setBlendMode 要求在 API 级别 29 中引入,这就是它在我运行 Android 7 的 V10 上不起作用的原因。我发现的解决方案是简单地使用较旧的 Paint.setXfermode,并选择 PorterDuff 乘法模式。

```java
Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));

参见:https://developer.android.com/reference/android/graphics/PorterDuff.Mode

英文:

*Solved

So apparently setBlendMode requires was introduced in API-level 29 and this is the reason it didn't work on my V10 which is running Android 7. The solution that i discovered was to simply use the older Paint.setXfermode with PorterDuff Multiply mode.

Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));

See: https://developer.android.com/reference/android/graphics/PorterDuff.Mode

huangapple
  • 本文由 发表于 2020年6月29日 16:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/62634249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定