问题描述
当试图模糊我要使用的图像时
When trying to blur an image I want to use
import android.support.v8.renderscript.*
所以我把它放在了build.gradle文件中:
So I put this in my build.gradle file:
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'"
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
renderscriptTargetApi 18
renderscriptSupportModeEnabled true
}}
我使用这种方法来模糊图像:
And I use this method to blur the image:
public static Bitmap blur(Context ctx, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(ctx);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
但是代码在此行崩溃:
RenderScript rs = RenderScript.create(ctx);
给出此错误:
03-03 14:40:02.965 27865-27865/com.myorder.app E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.support.v8.renderscript.RenderScript
at com.myorder.profile.BlurBuilder.blur(BlurBuilder.java:27)
at com.myorder.ui.SuggestionsHome$1.onResponse(SuggestionsHome.java:70)
at com.myorder.ui.SuggestionsHome$1.onResponse(SuggestionsHome.java:59)
at com.myorder.core.api.dao.impl.AuthDAOImpl$1.onResponse(AuthDAOImpl.java:75)
at com.myorder.core.api.dao.impl.AuthDAOImpl$1.onResponse(AuthDAOImpl.java:62)
at mobvolley.JacksonRequest.deliverResponse(JacksonRequest.java:84)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
我不知道为什么要跟踪这里的所有内容: http://developer.android.com/guide/topics/renderscript/compute.html
I don;t know why as I have followed everything from here: http://developer.android.com/guide/topics/renderscript/compute.html
任何人都可以帮忙吗?
推荐答案
我有一个带有应用程序模块和库模块的项目.
I have a project with an app module and a library module.
为了能够导入support.v8类,我在库模块build.gradle文件中使用以下命令声明了Renderscript的使用:
In order to be able to import the support.v8 classes, i declared the use of Renderscript in the library module build.gradle file using :
renderscriptTargetApi 23
renderscriptSupportModeEnabled true
这对编译很有帮助,但仍会导致 NoClassDefFoundError .
This is good for compilation, but still causes the NoClassDefFoundError.
然后我在应用程序模块build.gradle文件中再次声明了Renderscript的使用,现在可以正常使用了! (已在同时运行API 23的仿真器和Nexus 5x设备上进行了测试.)
I then declared again the use of Renderscript in the app module build.gradle file and now it works ! (tested with the emulator and a Nexus 5x device both running API 23).
这篇关于尝试模糊背景图像时,找不到RenderScript没有类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!