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

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

Android Paint.setBlendMode Crash

问题

  1. 我正在使用绘图工具paint在画布上绘制一些文本上方的突出显示框highlight boxes)。我需要将绘图工具的混合模式blendmode设置为multiply
  2. ```java
  3. Paint paint = new Paint();
  4. paint.setColor(Color.YELLOW);
  5. paint.setBlendMode(BlendMode.MULTIPLY);

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

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

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

build.gradle:

  1. apply plugin: 'com.android.application';
  2. android {
  3. compileSdkVersion 29
  4. buildToolsVersion "29.0.3"
  5. defaultConfig {
  6. applicationId "com.example.test"
  7. minSdkVersion 24
  8. targetSdkVersion 29
  9. multiDexEnabled true
  10. versionCode 1
  11. versionName "1.0"
  12. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  18. }
  19. }
  20. }
  21. dependencies {
  22. implementation fileTree(dir: 'libs', include: ['*.jar'])
  23. implementation 'androidx.appcompat:appcompat:1.1.0'
  24. implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  25. implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  26. testImplementation 'junit:junit:4.12'
  27. androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  28. androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  29. implementation 'com.google.android.material:material:1.3.0-alpha01'
  30. implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
  31. implementation 'com.tom_roush:pdfbox-android:1.8.10.1'
  32. implementation files('libs\\YouTubeAndroidPlayerApi.jar')
  33. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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);

  1. This however, causes a crash:
  2. **java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendMode;**
  3. It doesn&#39;t happen on my Oneplus 6T where it works perfectly. On my LG V10 however, this crash happens.
  4. Any ideas on how to fix this?
  5. **build.gradle:**

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

  1. defaultConfig {
  2. applicationId &quot;com.example.test&quot;
  3. minSdkVersion 24
  4. targetSdkVersion 29
  5. multiDexEnabled true
  6. versionCode 1
  7. versionName &quot;1.0&quot;
  8. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  9. }
  10. buildTypes {
  11. release {
  12. minifyEnabled false
  13. proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
  14. }
  15. }

}

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

  1. implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
  2. implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
  3. implementation &#39;androidx.legacy:legacy-support-v4:1.0.0&#39;
  4. testImplementation &#39;junit:junit:4.12&#39;
  5. androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
  6. androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;
  7. implementation &#39;com.google.android.material:material:1.3.0-alpha01&#39;
  8. implementation &#39;com.github.barteksc:android-pdf-viewer:3.2.0-beta.1&#39;
  9. implementation &#39;com.tom_roush:pdfbox-android:1.8.10.1&#39;
  10. implementation files(&#39;libs\\YouTubeAndroidPlayerApi.jar&#39;)

}

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. ***已解决***
  5. 显然,setBlendMode 要求在 API 级别 29 中引入,这就是它在我运行 Android 7 的 V10 上不起作用的原因。我发现的解决方案是简单地使用较旧的 Paint.setXfermode,并选择 PorterDuff 乘法模式。
  6. ```java
  7. 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.

  1. 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:

确定