如何在android中模糊imageview

如何在android中模糊imageview,android,Android,我有一个imageview,我以编程方式设置图像资源,如下所示: int resourceId = getResources().getIdentifier("imagename", "drawable", "mypackage"); imgLock.setImageResource(resourceId); 有什么简单的方法可以用模糊图像显示我的ImageView吗?您可以使用来说明这一点,也可以使用库在图像中产生模糊效果 stackblur库的使用: int resourceId = ge

我有一个imageview,我以编程方式设置图像资源,如下所示:

int resourceId = getResources().getIdentifier("imagename", "drawable", "mypackage");
imgLock.setImageResource(resourceId);
有什么简单的方法可以用模糊图像显示我的ImageView吗?

您可以使用来说明这一点,也可以使用库在图像中产生模糊效果

stackblur库的使用:

int resourceId = getResources().getIdentifier("imagename", "drawable", "mypackage");
// get bitmap from resource id
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
StackBlurManager stackBlurManager = new StackBlurManager(bitmap);
// process the image using a certain radius
stackBlurManager.process(progress*4);
// obtain the image and load it into an ImageView or any other component
imgLock.setImageBitmap(stackBlurManager.returnBlurredImage());
您可以使用来按照说明进行调整,也可以使用库在图像中产生模糊效果

stackblur库的使用:

int resourceId = getResources().getIdentifier("imagename", "drawable", "mypackage");
// get bitmap from resource id
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
StackBlurManager stackBlurManager = new StackBlurManager(bitmap);
// process the image using a certain radius
stackBlurManager.process(progress*4);
// obtain the image and load it into an ImageView or any other component
imgLock.setImageBitmap(stackBlurManager.returnBlurredImage());
}

}

}


}

}

}


这是一种简单的方法

使用alpha设置模糊颜色

public class BlurImageView extends ImageView {
    Paint rectPaint;

  private  int blurcolor=Color.parseColor("#aeffffff");
    public BlurImageView(Context context) {
        this(context, null);

    }

    public BlurImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public BlurImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        rectPaint=new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(blurcolor);
        invalidate();
    }

    public void setBlurcolor(int blurcolor) {
        this.blurcolor = blurcolor;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.i("BlurImageView","canvas");

        canvas.drawRect(getLeft(),0,getRight(),getHeight(),rectPaint);
    }
}
这是一种简单的方法

使用alpha设置模糊颜色

public class BlurImageView extends ImageView {
    Paint rectPaint;

  private  int blurcolor=Color.parseColor("#aeffffff");
    public BlurImageView(Context context) {
        this(context, null);

    }

    public BlurImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public BlurImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        rectPaint=new Paint();
        rectPaint.setAntiAlias(true);
        rectPaint.setStyle(Paint.Style.FILL);
        rectPaint.setColor(blurcolor);
        invalidate();
    }

    public void setBlurcolor(int blurcolor) {
        this.blurcolor = blurcolor;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.i("BlurImageView","canvas");

        canvas.drawRect(getLeft(),0,getRight(),getHeight(),rectPaint);
    }
}

可以使用滑动变换 您可以用一行代码模糊图像

Glide.with(this).load(R.drawable.demo)
    .bitmapTransform(new BlurTransformation(context))
    .into((ImageView) findViewById(R.id.image));

可以使用滑动变换 您可以用一行代码模糊图像

Glide.with(this).load(R.drawable.demo)
    .bitmapTransform(new BlurTransformation(context))
    .into((ImageView) findViewById(R.id.image));
有一种方法可以使用RenderScript,因此模糊速度极快且非常易于使用:

<ru.egslava.blurredview.BlurredImageView
    ...
    android:src="@drawable/..."
    app:radius="0.3"
    app:keepOriginal="true"
    app:downSampling="2" />

可以使用RenderScript进行模糊处理,因此模糊处理速度极快且非常易于使用:

<ru.egslava.blurredview.BlurredImageView
    ...
    android:src="@drawable/..."
    app:radius="0.3"
    app:keepOriginal="true"
    app:downSampling="2" />

在android中,有不同的方法可以使视图模糊,但我发现了使用Fresco库使视图模糊的最简单、最快的方法

在模块的build.gradle中添加以下依赖项

   compile 'jp.wasabeef:fresco-processors:2.1.0'
以及活动的onCreate()内部

        Fresco.initialize(this);
    setContentView(R.layout.activity_main);

    SimpleDraweeView  simpleDraweeView = (SimpleDraweeView) findViewById(R.id.sdv_image);

    //INSTANTIATE BLUR POST PROCESSOR
    Postprocessor  postprocessor = new BlurPostprocessor(this, BLUR_PRECENTAGE);

    //INSTATNTING IMAGE REQUEST USING POST PROCESSOR AS PARAMETER
    ImageRequest  imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(IMAGE_URL))
            .setPostprocessor(postprocessor)
            .build();

    //INSTANTATE CONTROLLOR()
    PipelineDraweeController  controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
            .setImageRequest(imageRequest)
            .setOldController(simpleDraweeView.getController())
            .build();

    //LOAD BLURRED IMAGE ON SimpleDraweeView(VIEW)
    simpleDraweeView.setController(controller);
如果您需要完整的实施,请访问此博客

在android中,有不同的方法可以使视图模糊,但我发现了使用Fresco library使视图模糊的最简单、最快速的方法

在模块的build.gradle中添加以下依赖项

   compile 'jp.wasabeef:fresco-processors:2.1.0'
以及活动的onCreate()内部

        Fresco.initialize(this);
    setContentView(R.layout.activity_main);

    SimpleDraweeView  simpleDraweeView = (SimpleDraweeView) findViewById(R.id.sdv_image);

    //INSTANTIATE BLUR POST PROCESSOR
    Postprocessor  postprocessor = new BlurPostprocessor(this, BLUR_PRECENTAGE);

    //INSTATNTING IMAGE REQUEST USING POST PROCESSOR AS PARAMETER
    ImageRequest  imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(IMAGE_URL))
            .setPostprocessor(postprocessor)
            .build();

    //INSTANTATE CONTROLLOR()
    PipelineDraweeController  controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
            .setImageRequest(imageRequest)
            .setOldController(simpleDraweeView.getController())
            .build();

    //LOAD BLURRED IMAGE ON SimpleDraweeView(VIEW)
    simpleDraweeView.setController(controller);
如果您需要完整的实施,请访问此博客

导入android.renderscript.Allocation;
导入android.renderscript.Element;
导入android.renderscript.renderscript;
导入android.renderscript.ScriptIntrinsicBlur;
位图模糊=blurRenderScript(这是您的位图,25);
//第二个参数是半径
yourImageView.setImageBitmap(模糊);
@SuppressLint(“新API”)
公共静态位图blurRenderScript(上下文上下文、位图smallBitmap、整数半径){
试一试{
smallBitmap=RGB565到RGB888(smallBitmap);
}捕获(例外e){
e、 printStackTrace();
}
位图位图=位图.createBitmap(
smallBitmap.getWidth(),smallBitmap.getHeight(),
位图.Config.ARGB_8888);
RenderScript RenderScript=RenderScript.create(上下文);
Allocation blurInput=Allocation.createFromBitmap(renderScript,smallBitmap);
Allocation blurOutput=Allocation.createFromBitmap(renderScript,位图);
ScriptIntrinsicBlur blur=ScriptIntrinsicBlur.create(renderScript,
元素U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(radius);//半径必须为0import android.renderscript.Allocation;
导入android.renderscript.Element;
导入android.renderscript.renderscript;
导入android.renderscript.ScriptIntrinsicBlur;
位图模糊=blurRenderScript(这是您的位图,25);
//第二个参数是半径
yourImageView.setImageBitmap(模糊);
@SuppressLint(“新API”)
公共静态位图blurRenderScript(上下文上下文、位图smallBitmap、整数半径){
试一试{
smallBitmap=RGB565到RGB888(smallBitmap);
}捕获(例外e){
e、 printStackTrace();
}
位图位图=位图.createBitmap(
smallBitmap.getWidth(),smallBitmap.getHeight(),
位图.Config.ARGB_8888);
RenderScript RenderScript=RenderScript.create(上下文);
Allocation blurInput=Allocation.createFromBitmap(renderScript,smallBitmap);
Allocation blurOutput=Allocation.createFromBitmap(renderScript,位图);
ScriptIntrinsicBlur blur=ScriptIntrinsicBlur.create(renderScript,
元素U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(radius);//半径必须为0
compile 'jp.wasabeef:fresco-processors:2.1.0'
在布局文件中使用以下代码:

<com.facebook.drawee.view.SimpleDraweeView
   android:id="@+id/imageView"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>
添加依赖项

compile 'jp.wasabeef:fresco-processors:2.1.0'
在布局文件中使用以下代码:

<com.facebook.drawee.view.SimpleDraweeView
   android:id="@+id/imageView"
   android:layout_width="match_parent"
   android:layout_height="match_parent"/>

只要使用这个库就可以了 ,我在BlurTransformation类中遇到了问题,并且出现了错误,这就是为什么不能使用Glide转换,但这很好

BlurImage.withContext(this)
     .blurFromResource(R.drawable.YOUR_RESOURCE)
     .into(imageView);

只要使用这个库就可以了 ,我在BlurTransformation类中遇到了问题,并且出现了错误,这就是为什么不能使用Glide转换,但这很好

BlurImage.withContext(this)
     .blurFromResource(R.drawable.YOUR_RESOURCE)
     .into(imageView);

有许多可用的库,您可以使用其中的任何一个。 我更喜欢图书馆。 它非常简单和优化

依赖关系:

 dependencies {
    compile 'jp.wasabeef:blurry:4.x.x'
}
功能

  • 模糊。带有(上下文)。半径(25)。采样(2)。转到(rootView)
  • //从视图中
    模糊。带有(上下文)。捕获(视图)。进入(图像视图)
  • //来自位图
    blury.with(上下文).from(位图).into(图像视图)
模糊选项

  • 半径
  • 下采样
  • 滤色器
  • 异步支持
  • 动画(仅覆盖)
直接获取位图

// Sync
val bitmap = Blurry.with(this)
  .radius(10)
  .sampling(8)
  .capture(findViewById(R.id.right_bottom)).get()
imageView.setImageDrawable(BitmapDrawable(resources, bitmap))

// Async
Blurry.with(this)
  .radius(25)
  .sampling(4)
  .color(Color.argb(66, 255, 255, 0))
  .capture(findViewById(R.id.left_bottom))
  .getAsync {
    imageView.setImageDrawable(BitmapDrawable(resources, it))
  }

有许多可用的库,您可以使用其中的任何一个。 我更喜欢图书馆。 它非常简单和优化

依赖关系:

 dependencies {
    compile 'jp.wasabeef:blurry:4.x.x'
}
功能

  • 模糊。带有(上下文)。半径(25)。采样(2)。转到(rootView)
  • //从视图中
    模糊。带有(上下文)。捕获(视图)。进入(图像视图)
  • //来自位图
    blury.with(上下文).from(位图).into(图像视图)
模糊选项

  • 半径
  • 下采样
  • 滤色器
  • 异步支持
  • 动画(仅覆盖)
直接获取位图

// Sync
val bitmap = Blurry.with(this)
  .radius(10)
  .sampling(8)
  .capture(findViewById(R.id.right_bottom)).get()
imageView.setImageDrawable(BitmapDrawable(resources, bitmap))

// Async
Blurry.with(this)
  .radius(25)
  .sampling(4)
  .color(Color.argb(66, 255, 255, 0))
  .capture(findViewById(R.id.left_bottom))
  .getAsync {
    imageView.setImageDrawable(BitmapDrawable(resources, it))
  }

还可以使用库模糊图像视图


还可以使用库模糊图像视图

最初回答