模糊的联系。 Android应用 [英] Blur on touch. Android application

查看:587
本文介绍了模糊的联系。 Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使其中图像用户的部分谈到了Android应用程序变得模糊的应用程序。

I am trying to make an application in which the part of the image user touched on the android app gets blurred.

要求是一样的,我应该能够采取一个单元,然后滑过我的手指,我需要模糊了点。有没有简单的方法吧。我能以某种方式使用透明漆不透明度来完成它?

Requirement is like, I should be able to take a snap and then slide over my finger over the points which I need to blur out. Is there any easy way for it. Can I somehow use transparent paint with opacity to accomplish it?

感谢

推荐答案

看来问题应该使用的 RenderScript 。然而,似乎它仅支持位图模糊,所以我们可能需要模糊之前削减位图。谈到这个答案如何快速的图像模糊,我是能够实现在Nexus 10出乎意料的好业绩平板电脑。

Seems the issue should be solved using RenderScript. However, seems that it supports only bitmap blur, so we might need to cut bitmap before blurring. Referring to this answer about fast image blur, I was able to achieve surprisingly good performance on Nexus 10 tablet.

在code为以下(注意:它的草案版本,我只有30分钟,发挥它):

The code is following (note: it's draft version, I've played with it only for 30 minutes):

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 创建位图会造成很大的滞后,在活动只有这种观点模糊区移动非常顺利。

Although I was afraid that creating Bitmap in onDraw will cause big lags, in activity with only this view blur area moves quite smoothly.

这篇关于模糊的联系。 Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆