java.lang.StackOverflowError:堆栈大小8MB

时间:2016-05-06 06:31:08

标签: java android android-layout canvas blur

我正在使用this library模糊我在android中布局下方的区域。 当调用模糊视图时,我得到stackoverflowerror和活动崩溃。请帮忙。 感谢。

以下是使用logcat的代码

BlurringView活动

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.util.AttributeSet;
import android.view.View;

/**
 * A custom view for presenting a dynamically blurred version of another view's content.
 * <p/>
 * Use {@link #setBlurredView(android.view.View)} to set up the reference to the view to be blurred.
 * After that, call {@link #invalidate()} to trigger blurring whenever necessary.
 */
public class BlurringView extends View {

    public BlurringView(Context context) {
        this(context, null);
    }

    public BlurringView(Context context, AttributeSet attrs) {
        super(context, attrs);

        final Resources res = getResources();
        final int defaultBlurRadius = res.getInteger(R.integer.default_blur_radius);
        final int defaultDownsampleFactor = res.getInteger(R.integer.default_downsample_factor);
        final int defaultOverlayColor = res.getColor(R.color.default_overlay_color);

        initializeRenderScript(context);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PxBlurringView);
        setBlurRadius(a.getInt(R.styleable.PxBlurringView_blurRadius, defaultBlurRadius));
        setDownsampleFactor(a.getInt(R.styleable.PxBlurringView_downsampleFactor,
                defaultDownsampleFactor));
        setOverlayColor(a.getColor(R.styleable.PxBlurringView_overlayColor, defaultOverlayColor));
        a.recycle();
    }

    public void setBlurredView(View blurredView) {
        mBlurredView = blurredView;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBlurredView != null) {
            if (prepare()) {
                // If the background of the blurred view is a color drawable, we use it to clear
                // the blurring canvas, which ensures that edges of the child views are blurred
                // as well; otherwise we clear the blurring canvas with a transparent color.
                if (mBlurredView.getBackground() != null && mBlurredView.getBackground() instanceof ColorDrawable) {
                    mBitmapToBlur.eraseColor(((ColorDrawable) mBlurredView.getBackground()).getColor());
                } else {
                    mBitmapToBlur.eraseColor(Color.TRANSPARENT);
                }

                mBlurredView.draw(mBlurringCanvas);
                blur();
                mBlurredView.invalidate();

                canvas.save();
                canvas.translate(mBlurredView.getX() - getX(), mBlurredView.getY() - getY());
                canvas.scale(mDownsampleFactor, mDownsampleFactor);
                canvas.drawBitmap(mBlurredBitmap, 0, 0, null);
                canvas.restore();
            }
            canvas.drawColor(mOverlayColor);
        }
    }

    public void setBlurRadius(int radius) {
        mBlurScript.setRadius(radius);
    }

    public void setDownsampleFactor(int factor) {
        if (factor <= 0) {
            throw new IllegalArgumentException("Downsample factor must be greater than 0.");
        }

        if (mDownsampleFactor != factor) {
            mDownsampleFactor = factor;
            mDownsampleFactorChanged = true;
        }
    }

    public void setOverlayColor(int color) {
        mOverlayColor = color;
    }

    private void initializeRenderScript(Context context) {
        mRenderScript = RenderScript.create(context);
        mBlurScript = ScriptIntrinsicBlur.create(mRenderScript, Element.U8_4(mRenderScript));
    }

    protected boolean prepare() {
        final int width = mBlurredView.getWidth();
        final int height = mBlurredView.getHeight();

        if (mBlurringCanvas == null || mDownsampleFactorChanged
                || mBlurredViewWidth != width || mBlurredViewHeight != height) {
            mDownsampleFactorChanged = false;

            mBlurredViewWidth = width;
            mBlurredViewHeight = height;

            int scaledWidth = width / mDownsampleFactor;
            int scaledHeight = height / mDownsampleFactor;

            // The following manipulation is to avoid some RenderScript artifacts at the edge.
            scaledWidth = scaledWidth - scaledWidth % 4 + 4;
            scaledHeight = scaledHeight - scaledHeight % 4 + 4;

            if (mBlurredBitmap == null
                    || mBlurredBitmap.getWidth() != scaledWidth
                    || mBlurredBitmap.getHeight() != scaledHeight) {
                mBitmapToBlur = Bitmap.createBitmap(scaledWidth, scaledHeight,
                        Bitmap.Config.ARGB_8888);
                if (mBitmapToBlur == null) {
                    return false;
                }

                mBlurredBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight,
                        Bitmap.Config.ARGB_8888);
                if (mBlurredBitmap == null) {
                    return false;
                }
            }

            mBlurringCanvas = new Canvas(mBitmapToBlur);
            mBlurringCanvas.scale(1f / mDownsampleFactor, 1f / mDownsampleFactor);
            mBlurInput = Allocation.createFromBitmap(mRenderScript, mBitmapToBlur,
                    Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
            mBlurOutput = Allocation.createTyped(mRenderScript, mBlurInput.getType());
        }
        return true;
    }

    protected void blur() {
        mBlurInput.copyFrom(mBitmapToBlur);
        mBlurScript.setInput(mBlurInput);
        mBlurScript.forEach(mBlurOutput);
        mBlurOutput.copyTo(mBlurredBitmap);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mRenderScript != null) {
            mRenderScript.destroy();
        }
    }

    private int mDownsampleFactor;
    private int mOverlayColor;

    private View mBlurredView;
    private int mBlurredViewWidth, mBlurredViewHeight;

    private boolean mDownsampleFactorChanged;
    private Bitmap mBitmapToBlur, mBlurredBitmap;
    private Canvas mBlurringCanvas;
    private RenderScript mRenderScript;
    private ScriptIntrinsicBlur mBlurScript;
    private Allocation mBlurInput, mBlurOutput;

    }

MyActivity类

BlurringView blurringView = (BlurringView) addView.findViewById(R.id.circle);
blurringView.setBlurredView(blurringView);
        blurringView.invalidate();

Logcat错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.designsample, PID: 19998
java.lang.StackOverflowError: stack size 8MB
at android.graphics.Canvas.drawOval(Canvas.java:1155)
at android.graphics.Canvas.drawOval(Canvas.java:1147)
at android.graphics.drawable.GradientDrawable.draw(GradientDrawable.java:615)
at android.view.View.drawBackground(View.java:16376)
at android.view.View.draw(View.java:16175)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundredpx.android.blur.BlurringView.onDraw(BlurringView.java:65)
at android.view.View.draw(View.java:16184)
at com.fivehundred
05-05 23:16:31.323 19998-19998/com.example.user.humandesignsample E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size =
6333780)
05-05 23:16:31.361 19998-19998/com.example.user.humandesignsample         E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException: data parcel size 6333780 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

2 个答案:

答案 0 :(得分:3)

问题在于这一行:

blurringView.setBlurredView(blurringView);

您正在为自己分配BlurringView。它没有任何意义。我假设(我不想阅读整个BlurringView类),setBlurredView用于您想要模糊的ViewBlurringView将充当blur叠加层。因此,将blur叠加层指定为"blurred"根本没有任何意义。

答案 1 :(得分:1)

尝试这样

blurringView.setBlurredView(the_view_which_you_want_to_blur);

就是这样