11package r12 .shadowframelayout ;
22
33import android .content .Context ;
4+ import android .content .res .Resources ;
45import android .content .res .TypedArray ;
56import android .graphics .Canvas ;
67import android .graphics .drawable .Drawable ;
78import android .os .Build ;
89import android .support .annotation .AttrRes ;
10+ import android .support .annotation .DimenRes ;
911import android .support .annotation .NonNull ;
1012import android .support .annotation .Nullable ;
1113import android .support .annotation .RequiresApi ;
1214import android .support .annotation .StyleRes ;
1315import android .support .v4 .content .ContextCompat ;
1416import android .util .AttributeSet ;
17+ import android .util .DisplayMetrics ;
1518import android .widget .FrameLayout ;
1619
1720/**
@@ -34,6 +37,8 @@ public class ShadowFrameLayout extends FrameLayout {
3437 private Drawable mShadowRightDrawable ;
3538 private int mShadowRightHeight ;
3639
40+ private Resources mResources ;
41+ private DisplayMetrics mDisplayMetrics ;
3742
3843 public ShadowFrameLayout (@ NonNull Context context ) {
3944 super (context );
@@ -56,7 +61,7 @@ public ShadowFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs,
5661 init (context , attrs );
5762 }
5863
59- private void init (Context context , AttributeSet attrs ) {
64+ private void init (@ NonNull Context context , @ Nullable AttributeSet attrs ) {
6065 if (attrs != null ) {
6166 TypedArray styledAttrs = context .obtainStyledAttributes (attrs , R .styleable .ShadowFrameLayout );
6267 mShadowTopHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowTopHeight , DEFAULT_SHADOW_HEIGHT );
@@ -65,11 +70,13 @@ private void init(Context context, AttributeSet attrs) {
6570 mShadowRightHeight = styledAttrs .getDimensionPixelSize (R .styleable .ShadowFrameLayout_shadowRightHeight , DEFAULT_SHADOW_HEIGHT );
6671 styledAttrs .recycle ();
6772 }
73+ mResources = context .getResources ();
74+ mDisplayMetrics = mResources .getDisplayMetrics ();
6875
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 );
76+ mShadowTopDrawable = ContextCompat .getDrawable (context , R .drawable .bg_top_shadow );
77+ mShadowBottomDrawable = ContextCompat .getDrawable (context , R .drawable .bg_bottom_shadow );
78+ mShadowLeftDrawable = ContextCompat .getDrawable (context , R .drawable .bg_left_shadow );
79+ mShadowRightDrawable = ContextCompat .getDrawable (context , R .drawable .bg_right_shadow );
7380 }
7481
7582 @ Override
@@ -89,12 +96,68 @@ private void drawShadow(Canvas canvas) {
8996 mShadowRightDrawable .draw (canvas );
9097 }
9198
92- public void setShadowsHeight (int left , int top , int right , int bottom ) {
93- mShadowLeftHeight = left ;
94- mShadowTopHeight = top ;
95- mShadowRightHeight = right ;
96- mShadowBottomHeight = bottom ;
99+ public void setShadowsHeightInPixels (int left , int top , int right , int bottom ) {
100+ mShadowLeftHeight = convertPixelsToDp ( left ) ;
101+ mShadowTopHeight = convertPixelsToDp ( top ) ;
102+ mShadowRightHeight = convertPixelsToDp ( right ) ;
103+ mShadowBottomHeight = convertPixelsToDp ( bottom ) ;
97104 invalidate ();
98105 }
99106
107+ public void setShadowsHeightDimens (@ DimenRes int left , @ DimenRes int top , @ DimenRes int right , @ DimenRes int bottom ) {
108+ mShadowLeftHeight = mResources .getDimensionPixelOffset (left );
109+ mShadowTopHeight = mResources .getDimensionPixelOffset (top );
110+ mShadowRightHeight = mResources .getDimensionPixelOffset (right );
111+ mShadowBottomHeight = mResources .getDimensionPixelOffset (bottom );
112+ invalidate ();
113+ }
114+
115+ public void setLeftShadowInPixels (int size ) {
116+ mShadowLeftHeight = convertPixelsToDp (size );
117+ invalidate ();
118+ }
119+
120+ public void setLeftShadowDimens (@ DimenRes int resource ) {
121+ mShadowLeftHeight = mResources .getDimensionPixelOffset (resource );
122+ invalidate ();
123+ }
124+
125+ public void setRightShadowInPixels (int size ) {
126+ mShadowRightHeight = convertPixelsToDp (size );
127+ invalidate ();
128+ }
129+
130+ public void setRightShadowDimens (@ DimenRes int resource ) {
131+ mShadowRightHeight = mResources .getDimensionPixelOffset (resource );
132+ invalidate ();
133+ }
134+
135+ public void setTopShadowInPixels (int size ) {
136+ mShadowTopHeight = convertPixelsToDp (size );
137+ invalidate ();
138+ }
139+
140+ public void setTopShadowDimens (@ DimenRes int resource ) {
141+ mShadowTopHeight = mResources .getDimensionPixelOffset (resource );
142+ invalidate ();
143+ }
144+
145+ public void setBottomShadowInPixels (int size ) {
146+ mShadowBottomHeight = convertPixelsToDp (size );
147+ invalidate ();
148+ }
149+
150+ public void setBottomShadowDimens (@ DimenRes int resource ) {
151+ mShadowBottomHeight = mResources .getDimensionPixelOffset (resource );
152+ invalidate ();
153+ }
154+
155+ private int convertPixelsToDp (int px ) {
156+ return px / (mDisplayMetrics .densityDpi / DisplayMetrics .DENSITY_DEFAULT );
157+ }
158+
159+ private int convertDpToPixel (int dp ) {
160+ return dp * (mDisplayMetrics .densityDpi / DisplayMetrics .DENSITY_DEFAULT );
161+ }
162+
100163}
0 commit comments