模糊图像位图并将其设置为背景

时间:2019-08-15 13:53:53

标签: java android imageview blur blurry

我一直试图模糊图像视图,并将整个布局的背景设置为模糊位图,但无济于事。这些图片基本上显示了要实现的目标,这些图片仅模糊了imageView并将展开的模糊图像设置为布局背景。

enter image description here

enter image description here

int color = getDominantColor(bitmap); //get dominant color of the bitmap

 //create gradient
                GradientDrawable gradient = new GradientDrawable(
                        GradientDrawable.Orientation.TOP_BOTTOM,
                        new int[] {0xf5f5f5,color});
                gradient.setCornerRadius(0f);

                //setbackground of the relativelayout
                rl.setBackground(gradient);//rl is relative layout





  //getting dominant color of bitmap
public static int getDominantColor(Bitmap bitmap) {
    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    final int color = newBitmap.getPixel(0, 0);
    newBitmap.recycle();
    return color;
}

我不希望tp将背景设置为渐变,但是作为该图像位图的模糊结果,就像在我包含的图像中一样。

1 个答案:

答案 0 :(得分:0)

有很多选择:

  1. 您可以放大然后缩小位图。这样做是减少产生类似模糊效果的图片数量。如果要执行此操作,请参见此处:Fast Bitmap Blur For Android SDK。但是,它的内存使用效率不是很高,如果可以这样实现,则可能需要更改实现。
  2. 使用预制库。有很多库,其中一些比其他的要好,这使您可以模糊位图。制作这些库的人已经在减少内存使用和更好的兼容性方面做出了艰苦的工作。这里也有一个示例:Fast Bitmap Blur For Android SDK

注意:我没有发布任何代码,因为解决方案的一部分是库或自定义类。

希望有帮助。