2121public class ShadowFrameLayout extends FrameLayout {
2222
2323 private static final int DEFAULT_SHADOW_HEIGHT = 0 ;
24- private Drawable mTopDrawable ;
25- private int mTopShadowHeight ;
26- private Drawable mBottomDrawable ;
27- private int mBottomShadowHeight ;
24+
25+ private Drawable mShadowTopDrawable ;
26+ private int mShadowTopHeight ;
27+
28+ private Drawable mShadowBottomDrawable ;
29+ private int mShadowBottomHeight ;
30+
31+ private Drawable mShadowLeftDrawable ;
32+ private int mShadowLeftHeight ;
33+
34+ private Drawable mShadowRightDrawable ;
35+ private int mShadowRightHeight ;
36+
2837
2938 public ShadowFrameLayout (@ NonNull Context context ) {
3039 super (context );
@@ -49,21 +58,43 @@ public ShadowFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs,
4958
5059 private void init (Context context , AttributeSet attrs ) {
5160 if (attrs != null ) {
52- TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .ShadowFrameLayout );
53- mTopShadowHeight = typedArray .getDimensionPixelOffset (R .styleable .ShadowFrameLayout_shadowTopHeight , DEFAULT_SHADOW_HEIGHT );
54- mBottomShadowHeight = typedArray .getDimensionPixelOffset (R .styleable .ShadowFrameLayout_shadowBottomHeight , DEFAULT_SHADOW_HEIGHT );
55- typedArray .recycle ();
61+ TypedArray styledAttrs = context .obtainStyledAttributes (attrs , R .styleable .ShadowFrameLayout );
62+ mShadowTopHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowTopHeight , DEFAULT_SHADOW_HEIGHT );
63+ mShadowBottomHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowBottomHeight , DEFAULT_SHADOW_HEIGHT );
64+ mShadowLeftHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowLeftHeight , DEFAULT_SHADOW_HEIGHT );
65+ mShadowRightHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowRightHeight , DEFAULT_SHADOW_HEIGHT );
66+ styledAttrs .recycle ();
5667 }
57- mTopDrawable = ContextCompat .getDrawable (context , R .drawable .bg_top_shadow );
58- mBottomDrawable = ContextCompat .getDrawable (context , R .drawable .bg_bottom_shadow );
68+
69+ mShadowTopDrawable = ContextCompat .getDrawable (getContext (), R .drawable .bg_top_shadow );
70+ mShadowBottomDrawable = ContextCompat .getDrawable (getContext (), R .drawable .bg_bottom_shadow );
71+ mShadowLeftDrawable = ContextCompat .getDrawable (getContext (), R .drawable .bg_left_shadow );
72+ mShadowRightDrawable = ContextCompat .getDrawable (getContext (), R .drawable .bg_right_shadow );
5973 }
6074
6175 @ Override
6276 protected void dispatchDraw (Canvas canvas ) {
6377 super .dispatchDraw (canvas );
64- mTopDrawable .setBounds (0 , 0 , canvas .getWidth (), mTopShadowHeight );
65- mTopDrawable .draw (canvas );
66- mBottomDrawable .setBounds (0 , canvas .getHeight () - mBottomShadowHeight , canvas .getWidth (), canvas .getHeight ());
67- mBottomDrawable .draw (canvas );
78+ drawShadow (canvas );
6879 }
80+
81+ private void drawShadow (Canvas canvas ) {
82+ mShadowTopDrawable .setBounds (0 , 0 , canvas .getWidth (), mShadowTopHeight );
83+ mShadowTopDrawable .draw (canvas );
84+ mShadowBottomDrawable .setBounds (0 , canvas .getHeight () - mShadowBottomHeight , canvas .getWidth (), canvas .getHeight ());
85+ mShadowBottomDrawable .draw (canvas );
86+ mShadowLeftDrawable .setBounds (0 , 0 , mShadowLeftHeight , canvas .getHeight ());
87+ mShadowLeftDrawable .draw (canvas );
88+ mShadowRightDrawable .setBounds (canvas .getWidth () - mShadowRightHeight , 0 , canvas .getWidth (), canvas .getHeight ());
89+ mShadowRightDrawable .draw (canvas );
90+ }
91+
92+ public void setShadowsHeight (int left , int top , int right , int bottom ) {
93+ mShadowLeftHeight = left ;
94+ mShadowTopHeight = top ;
95+ mShadowRightHeight = right ;
96+ mShadowBottomHeight = bottom ;
97+ invalidate ();
98+ }
99+
69100}
0 commit comments