Changing CardView shadow color

前端 未结 5 665
孤独总比滥情好
孤独总比滥情好 2020-12-23 17:44

This question was asked on SO many times, but still I didn\'t find a good solution for this problem.

Why do I need this to do? Well because project me and my team de

5条回答
  •  暖寄归人
    2020-12-23 18:34

    You Can Implement this without having a cardview, and can also have all the properties of cardview

    You have to Do:

    1. Copy the two classes

    2. Wrap your required view with the Custom View as in the example, you don't have to do much changes in your layout or anywhere else!

    The below class will create a custom view, this will be wrapping your layout/View to be displayed in cardview with custom shadow color

    Create a class:

    import android.content.Context;
    import android.support.annotation.Nullable;
    import android.support.v4.content.ContextCompat;
    import android.util.AttributeSet;
    import android.view.Gravity;
    import android.widget.LinearLayout;
    
    import com.qzion.nfscrew.R;
    
    
    public class RoundLinerLayoutNormal extends LinearLayout {
        public RoundLinerLayoutNormal(Context context) {
            super(context);
            initBackground();
        }
    
        public RoundLinerLayoutNormal(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initBackground();
        }
    
        public RoundLinerLayoutNormal(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initBackground();
        }
    
        private void initBackground() {
            setBackground(ViewUtils.generateBackgroundWithShadow(this,R.color.white,
                    R.dimen.radius_corner,R.color.colorPrimaryDark,R.dimen.elevation, Gravity.BOTTOM));
        }
    }
    

    Also create the class for the Shadow Settings, ViewUtils.java

    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.LayerDrawable;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.RoundRectShape;
    import android.support.annotation.ColorRes;
    import android.support.annotation.DimenRes;
    import android.support.v4.content.ContextCompat;
    import android.view.Gravity;
    import android.view.View;
    
    import static android.support.v4.view.ViewCompat.LAYER_TYPE_SOFTWARE;
    
    
    public class ViewUtils {
    
        public static Drawable generateBackgroundWithShadow(View view, @ColorRes int backgroundColor,
                                                            @DimenRes int cornerRadius,
                                                            @ColorRes int shadowColor,
                                                            @DimenRes int elevation,
                                                            int shadowGravity) {
            float cornerRadiusValue = view.getContext().getResources().getDimension(cornerRadius);
            int elevationValue = (int) view.getContext().getResources().getDimension(elevation);
            int shadowColorValue = ContextCompat.getColor(view.getContext(),shadowColor);
            int backgroundColorValue = ContextCompat.getColor(view.getContext(),backgroundColor);
    
            float[] outerRadius = {cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
                    cornerRadiusValue, cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
                    cornerRadiusValue};
    
            Paint backgroundPaint = new Paint();
            backgroundPaint.setStyle(Paint.Style.FILL);
            backgroundPaint.setShadowLayer(cornerRadiusValue, 0, 0, 0);
    
            Rect shapeDrawablePadding = new Rect();
            shapeDrawablePadding.left = elevationValue;
            shapeDrawablePadding.right = elevationValue;
    
            int DY;
            switch (shadowGravity) {
                case Gravity.CENTER:
                    shapeDrawablePadding.top = elevationValue;
                    shapeDrawablePadding.bottom = elevationValue;
                    DY = 0;
                    break;
                case Gravity.TOP:
                    shapeDrawablePadding.top = elevationValue*2;
                    shapeDrawablePadding.bottom = elevationValue;
                    DY = -1*elevationValue/3;
                    break;
                default:
                case Gravity.BOTTOM:
                    shapeDrawablePadding.top = elevationValue;
                    shapeDrawablePadding.bottom = elevationValue*2;
                    DY = elevationValue/3;
                    break;
            }
    
            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.setPadding(shapeDrawablePadding);
    
            shapeDrawable.getPaint().setColor(backgroundColorValue);
            shapeDrawable.getPaint().setShadowLayer(cornerRadiusValue/3, 0, DY, shadowColorValue);
    
            view.setLayerType(LAYER_TYPE_SOFTWARE, shapeDrawable.getPaint());
    
            shapeDrawable.setShape(new RoundRectShape(outerRadius, null, null));
    
            LayerDrawable drawable = new LayerDrawable(new Drawable[]{shapeDrawable});
            drawable.setLayerInset(0, elevationValue, elevationValue*2, elevationValue, elevationValue*2);
    
            return drawable;
    
        }
    }
    

    and finally your XML, where you have the views required to have shadow.

    
    
                    
    
                
    

提交回复
热议问题