简体   繁体   English

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

[英]Blur Image bitmap and set it as background

I have been trying to blur an imageview and set the background of the whole layout to the blurred bitmap to no avail.我一直在尝试模糊图像视图并将整个布局的背景设置为模糊的位图无济于事。 Basically what am trying to achieve is shown in these pictures, where they only blur the imageView and set the expanded blurred image as the layout background.基本上我试图实现的内容显示在这些图片中,它们只模糊 imageView 并将扩展的模糊图像设置为布局背景。

在此处输入图片说明

在此处输入图片说明

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;
}

I dont want tp set the background as a gradient, but as the blurred result of that image bitmap, just like in the images i've included.我不希望 tp 将背景设置为渐变,而是作为该图像位图的模糊结果,就像我包含的图像一样。

There are many options:有很多选择:

  1. You can enlarge and then downscale the bitmap.您可以放大然后缩小位图。 What that does is decrease the number of pictures producing something like the effect of blurring.这样做是减少产生模糊效果之类的图片数量。 See here if you want to do that: Fast Bitmap Blur For Android SDK .如果您想这样做,请参见此处: Android SDK 的快速位图模糊 However, it isn't very memory efficient and you may want to change your implementation if it works like this.但是,它的内存效率不是很高,如果它像这样工作,您可能需要更改您的实现。
  2. Use a pre-made library.使用预制库。 There are many libraries, some better than others, allowing you to blur bitmaps.有许多库,有些比其他库更好,允许您模糊位图。 The people who made these libraries have already done the hard job at reducing memory usage and better compatibility.制作这些库的人已经在减少内存使用和更好的兼容性方面做了艰苦的工作。 An example is also here: Fast Bitmap Blur For Android SDK这里还有一个例子: Fast Bitmap Blur For Android SDK

Note: I didn't post any code because part of the solution is libraries or custom classes.注意:我没有发布任何代码,因为解决方案的一部分是库或自定义类。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM