触摸时模糊。Android应用程序
我正在尝试制作一个应用程序,其中用户在android应用程序上触摸到的图像部分变得模糊 要求是,我应该能够拍一张快照,然后在需要模糊的点上滑动手指。有什么简单的方法吗。我可以用不透明的透明颜料来完成吗触摸时模糊。Android应用程序,android,blur,Android,Blur,我正在尝试制作一个应用程序,其中用户在android应用程序上触摸到的图像部分变得模糊 要求是,我应该能够拍一张快照,然后在需要模糊的点上滑动手指。有什么简单的方法吗。我可以用不透明的透明颜料来完成吗 谢谢看来这个问题应该通过使用解决。然而,它似乎只支持位图模糊,所以我们可能需要在模糊之前剪切位图。说到快速图像模糊,我在Nexus10平板电脑上获得了令人惊讶的好性能 代码如下(注意:这是草稿,我只玩了30分钟): 结果是: 虽然我担心在onDraw中创建Bitmap会造成很大的延迟,但在活动中
谢谢看来这个问题应该通过使用解决。然而,它似乎只支持位图模糊,所以我们可能需要在模糊之前剪切位图。说到快速图像模糊,我在Nexus10平板电脑上获得了令人惊讶的好性能 代码如下(注意:这是草稿,我只玩了30分钟): 结果是:
虽然我担心在
onDraw
中创建Bitmap
会造成很大的延迟,但在活动中,只有这个视图模糊区域移动得非常平稳。你尝试过什么吗?@ChintanRathod:我尝试过在图像上绘画。当我将手指滑动到图像上时,它会画在图像上。我想做的是以某种方式更改绘制对象设置,这样当我移动手指而不是绘制时,它只会模糊图像的一部分。你可以这样做,但为此你需要将两个位图一个放置在另一个上。正面将是原创的。后面的图像将是模糊的图像。尝试在触摸时搜索擦除图像
。这将为您提供如何擦除顶部图像,以便后面的图像显示出来,用户感觉图像变得模糊。:)嘿,你解决问题了吗?我需要同样的东西你能告诉我怎么用这个代码来模糊图像吗?有点不同,似乎还有另一个关于itI的问题我正在使用此代码在触摸屏上模糊图像,但它现在对我有效。我不知道如何使用此代码。我不知道从哪里可以获得完整的代码。你能帮我获得它吗?在回答中的示例中,完整的应用程序代码很简单,只有一个自定义视图的活动,仅此而已。
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);
}
}
}