触摸模糊. Android应用程序

触摸模糊. Android应用程序,第1张

概述我正在尝试制作一个应用程序,其中图像用户触摸Android应用程序的部分变得模糊.要求是这样的,我应该能够拍一下,然后用手指滑过我需要模糊的点.有没有简单的方法呢.我可以以某种方式使用不透明的透明涂料来完成它吗?谢谢解决方法:似乎问题应该使用RenderScript解决.但是,它似乎只支

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

要求是这样的,我应该能够拍一下,然后用手指滑过我需要模糊的点.有没有简单的方法呢.我可以以某种方式使用不透明的透明涂料来完成它吗?

谢谢

解决方法:

似乎问题应该使用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会导致很大的滞后,但在仅有此视图的活动中,模糊区域移动得相当顺利.

总结

以上是内存溢出为你收集整理的触摸模糊. Android应用程序全部内容,希望文章能够帮你解决触摸模糊. Android应用程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1119007.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存