Search code examples
androidbackground-colorblur

How to make a Blur background depends on the Image color that you are loading in Android


is there any systemical possible way (i mean in code) to make a blur background depends on the image user open, the color must be similar to the image that will open.

for example, the background on this page is grey.

enter image description here


Solution

  • You will need to get the dominant color from the image you are using then create a gradient drawable between a starting color and your dominant color. There are multiple ways to find dominant colors which you can read up on here: Finding the dominant color of an image in an Android @drawable

    From there you create a drawable and set the background of your view to that drawable:

        // get the drawable from the image view
        // could also be pulled from resources if available
        Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
    
        int color = getDominantColor(bm);
    
        GradientDrawable gradient = new GradientDrawable(
                GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {0xFFF,color});
        gradient.setCornerRadius(15f);
    
        contentView.setBackground(gradient);