Drop shadow blur effect on a FrameLayout / Layout in Android

3.6k views Asked by At

I'm trying to make a drop shadow blur effect WITH COLOR on a rectangular layout view. I've tried to use this code but to no avail.

    int glowRadius = 14;

    int glowColor = Color.parseColor("#acc5fe");

    Paint paint = new Paint();
    paint.setColor(glowColor);

    paint.setMaskFilter(new BlurMaskFilter(glowRadius, BlurMaskFilter.Blur.OUTER));
    RectF rectF = new RectF(mRootView.getHeight(), mRootView.getWidth(),
            mRootView.getHeight(), mRootView.getWidth());
    Canvas canvas = new Canvas();
    canvas.drawRect(rectF, paint);
    mRootView.draw(canvas);

It doesn't seem to do anything though. I also tried to use shadowDx and shadowDy etc. but that does nothing.

How to add a blurred drop shadow to a button? is a very similar question but I don't think the 9-patch is a viable solution since it's a layout and not an image.

How do you create a drop outer shadow blur effect on Android?

Update

Still haven't found a successful answer. I want something like

example blur

where the shadow effect is on the email edittext with a different color than black.

2

There are 2 answers

1
Mehul Kabaria On

try following A library for supporting convex material shadows

https://github.com/harjot-oberai/MaterialShadows

3
Vijay E On

What I understand from your question is first you want to drop a shadow. Here is what you can do to drop a shadow.
Just add this to your parent layout in xml.

android:background="@android:drawable/dialog_holo_light_frame"

Check this answer for more information. how to add shadow effect in alert dialog box in android
Let me know if this works for you.

Edit: Please check this answer. This might help you. Extending Android View class to add a dropshadow
It can be achieved using NinePatchDrawable. Below is a simple example.

  protected NinePatchDrawable bg;
  protected Paint paint;
  protected Rect padding = new Rect();
  protected Bitmap bmp;

      protected void init() {
        // decode the 9patch drawable
        bg = (NinePatchDrawable) getResources().getDrawable(R.drawable.balloon);

        // get paddings from the 9patch and apply them to the View
        bg.getPadding(padding);
        setPadding(padding.left, padding.top, padding.right, padding.bottom);

        // prepare the Paint to use below
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.rgb(255,255,255));
        paint.setStyle(Style.FILL);

        // this check is needed in order to get this code
        // working if target SDK>=11
        if( Build.VERSION.SDK_INT >= 11 )
          setLayerType(View.LAYER_TYPE_SOFTWARE, paint);

        // set the shadowLayer
        paint.setShadowLayer(
          padding.left * .2f, // radius
          0f, // blurX
          padding.left * .1f, // blurY
          Color.argb(128, 0, 0, 0) // shadow color
        );
      }

Also please try this color:

Color.argb(128, 0, 0, 0)