0

当试图模糊我想使用的图像时

import android.support.v8.renderscript.*

所以我把它放在我的 build.gradle 文件中:

android {
compileSdkVersion 23
buildToolsVersion '23.0.1'"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 23

    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true

}}

我使用这种方法来模糊图像:

    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

任何人都可以帮忙吗?

4

2 回答 2

1

我有一个带有应用程序模块和库模块的项目。

为了能够导入 support.v8 类,我在库模块 build.gradle 文件中声明了 Renderscript 的使用:

    renderscriptTargetApi 23
    renderscriptSupportModeEnabled true

这有利于编译,但仍会导致NoClassDefFoundError

然后我再次声明在应用程序模块 build.gradle 文件中使用 Renderscript,现在它可以工作了!(使用模拟器和运行 API 23 的 Nexus 5x 设备进行测试)。

于 2016-04-01T14:42:27.323 回答
0

这可能是由于 Proguard 剥离而发生的。有一些关于如何保留所有与 RS 相关的类/等的指南。包含在您的 .apk 中。

于 2016-03-03T20:42:07.953 回答