英文:
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'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'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 "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>
# 答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论