4

我正在尝试制作一个应用程序,其中用户在 android 应用程序上触摸的图像部分变得模糊。

要求就像,我应该能够拍一张照片,然后在我需要模糊的点上滑过我的手指。有什么简单的方法吗。我可以以某种方式使用具有不透明度的透明涂料来完成它吗?

谢谢

4

1 回答 1

4

似乎应该使用RenderScript解决问题。但是,它似乎只支持位图模糊,所以我们可能需要在模糊之前剪切位图。参考这个关于快速图像模糊的答案,我能够在 Nexus 10 平板电脑上实现令人惊讶的出色性能。

代码如下(注意:它是草稿版本,我只玩了 30 分钟):

public class BlurTouchImageView extends View {
    private static final int BLUR_RADIUS = 20;
    // TODO: resources should be used
    private static final int BLUR_SIDE = 300;

    private RenderScript mBlurScript = null;
    private ScriptIntrinsicBlur mIntrinsicScript = null;
    private Bitmap mBitmap = null;
    private Bitmap mBlurredBitmap = null;
    private float mX = -1;
    private float mY = -1;

    public BlurTouchImageView(final Context context) {
        super(context);
        init();
    }

    public BlurTouchImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BlurTouchImageView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * Inits our view internal members
     */
    private void init() {
        mBlurScript = RenderScript.create(getContext());
        mIntrinsicScript = ScriptIntrinsicBlur.create(mBlurScript, Element.U8_4(mBlurScript));
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gimg);
        mBlurredBitmap = Bitmap.createBitmap(BLUR_SIDE, BLUR_SIDE, mBitmap.getConfig());
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        boolean retval = false;

        mX = event.getX();
        mY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                retval = true;
                invalidate();
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mX = -1;
                mY = - 1;
                retval = true;
                invalidate();
                break;

            default:
                // nothing to do here
                break;
        }

        return retval;
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        // Blur bitmap if it's touched

        canvas.drawBitmap(mBitmap, 0, 0, null);

        if (mX > 0 && mY > 0) {
            // Yeah, it will slooow down drawing, but how else we can prepare needed part of bitmap?
            final Bitmap blurSource = Bitmap.createBitmap(mBitmap, (int) mX - BLUR_SIDE / 2, (int) mY - BLUR_SIDE / 2, BLUR_SIDE, BLUR_SIDE);

            final Allocation inAlloc = Allocation.createFromBitmap(mBlurScript, blurSource, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE);
            final Allocation outAlloc = Allocation.createFromBitmap(mBlurScript, mBlurredBitmap);

            mIntrinsicScript.setRadius(BLUR_RADIUS);
            mIntrinsicScript.setInput(inAlloc);
            mIntrinsicScript.forEach(outAlloc);
            outAlloc.copyTo(mBlurredBitmap);

            canvas.drawBitmap(mBlurredBitmap, (int)mX - BLUR_SIDE / 2, (int)mY - BLUR_SIDE / 2, null);
        }
    }
}

结果是:

模糊示例

虽然我担心创建BitmaponDraw导致很大的延迟,但只有这个视图模糊区域的活动非常顺利。

于 2014-12-17T01:22:51.223 回答