【问题标题】:Blur on touch. Android application触摸模糊。安卓应用
【发布时间】:2018-02-24 13:41:06
【问题描述】:

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

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

谢谢

【问题讨论】:

  • 你试过了吗?
  • @ChintanRathod:我尝试过在图像上绘画。当我将手指滑过图像时,它会在图像上绘制。我想要做的是以某种方式更改 Paint 对象设置,这样当我移动手指而不是绘制它时,只会模糊图像的一部分。
  • 您可以这样做,但为此您需要将 2 个位图放在另一个上。正面将是原始的。后图像将是模糊的图像。尝试搜索erase image on touch。这将为您提供如何擦除顶部图像,以便后部图像出现并且用户感觉图像变得模糊。 :)
  • 你解决了这个问题吗?我需要同样的东西

标签: android blur


【解决方案1】:

似乎应该使用RenderScript 解决该问题。但是,它似乎只支持位图模糊,所以我们可能需要在模糊之前剪切位图。参考this answer 关于快速图像模糊的信息,我能够在 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);
        }
    }
}

结果是:

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

【讨论】:

  • 您能告诉我如何使用此代码来模糊图像。
  • 有点不一样,应该还有个问题
  • 我正在使用此代码在触摸时模糊图像,但它现在对我有用。我无法理解如何使用它。
  • 我不知道从哪里得到完整的代码。你能帮我得到它吗。
  • 在答案中的示例中,完整的应用程序代码很简单,只有单个自定义视图的活动,仅此而已。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 2018-07-13
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多