1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package com.ruben.customviews;
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.widget.FrameLayout;
import com.namaste.hsswcom.rubens ShadowViewCard extends FrameLayout { private static final int DEFAULT_VALUE_SHADOW_COLOR = R.color.shadow_default_color; private static final int DEFAULT_VALUE_SHADOW_CARD_COLOR = R.color.shadow_card_default_color; private static final int DEFAULT_VALUE_SHADOW_CARD_PADDING_WIDTH = 0; private static final int DEFAULT_VALUE_SHADOW_CARD_PADDING_HEIGHT = 0; private static final int DEFAULT_VALUE_SHADOW_ROUND = 0;
private static final int DEFAULT_VALUE_SHADOW_RADIUS = 10; private static final int DEFAULT_VALUE_SHADOW_TOP_HEIGHT = 5; private static final int DEFAULT_VALUE_SHADOW_LEFT_HEIGHT = 5; private static final int DEFAULT_VALUE_SHADOW_RIGHT_HEIGHT = 5; private static final int DEFAULT_VALUE_SHADOW_BOTTOM_HEIGHT = 5; private static final int DEFAULT_VALUE_SHADOW_OFFSET_Y = 0; private static final int DEFAULT_VALUE_SHADOW_OFFSET_X = DEFAULT_VALUE_SHADOW_TOP_HEIGHT / 3;
private int shadowRound; private int shadowColor; private int shadowCardColor; private int shadowCardPaddingHeight; private int shadowCardPaddingWidth; private int shadowRadius; private int shadowOffsetY; private int shadowOffsetX; private int shadowTopHeight; private int shadowLeftHeight; private int shadowRightHeight; private int shadowBottomHeight;
public ShadowViewCard(Context context) { this(context, null); }
public ShadowViewCard(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public ShadowViewCard(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); }
private void initView(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShadowViewCard); shadowRound = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowRound, DEFAULT_VALUE_SHADOW_ROUND); shadowColor = a.getColor(R.styleable.ShadowViewCard_shadowColors, getResources().getColor(DEFAULT_VALUE_SHADOW_COLOR, null)); shadowCardColor = a.getColor(R.styleable.ShadowViewCard_shadowCardColor, getResources().getColor(DEFAULT_VALUE_SHADOW_CARD_COLOR, null)); shadowTopHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowTopHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_TOP_HEIGHT)); shadowRightHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowRightHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_RIGHT_HEIGHT)); shadowLeftHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowLeftHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_LEFT_HEIGHT)); shadowCardPaddingHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowCardPaddingHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_CARD_PADDING_HEIGHT)); shadowCardPaddingWidth = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowCardPaddingWidth, dp2px(getContext(), DEFAULT_VALUE_SHADOW_CARD_PADDING_WIDTH)); shadowBottomHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowBottomHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_BOTTOM_HEIGHT)); shadowOffsetY = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowOffsetY, dp2px(getContext(), DEFAULT_VALUE_SHADOW_OFFSET_Y)); shadowOffsetX = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowOffsetX, dp2px(getContext(), DEFAULT_VALUE_SHADOW_OFFSET_X)); shadowRadius = a.getInteger(R.styleable.ShadowViewCard_shadowRadius, DEFAULT_VALUE_SHADOW_RADIUS); a.recycle(); setPadding(shadowCardPaddingWidth, shadowCardPaddingHeight, shadowCardPaddingWidth, shadowCardPaddingHeight); setLayerType(LAYER_TYPE_SOFTWARE, null); }
public static int dp2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); }
@Override protected void dispatchDraw(Canvas canvas) { Paint shadowPaint = new Paint(); shadowPaint.setColor(shadowCardColor); shadowPaint.setStyle(Paint.Style.FILL); shadowPaint.setAntiAlias(true); float left = shadowLeftHeight + shadowCardPaddingWidth; float top = shadowTopHeight + shadowCardPaddingHeight; float right = getWidth() - shadowRightHeight - shadowCardPaddingWidth; float bottom = getHeight() - shadowBottomHeight - shadowCardPaddingHeight; shadowPaint.setShadowLayer(shadowRadius, shadowOffsetX, shadowOffsetY, shadowColor); RectF rectF = new RectF(left, top, right, bottom); canvas.drawRoundRect(rectF, shadowRound, shadowRound, shadowPaint); canvas.save(); super.dispatchDraw(canvas); } }
|