@@ -19,39 +19,19 @@ public class ScaleLayoutHelper {
1919 private static final String TAG = "ScaleLayoutHelper" ;
2020 private final ViewGroup mHost ;
2121
22- private float designWidth ;
23- private float designHeight ;
24- private float designDensity ;
22+ private ScaleLayoutInfo mHostLayoutInfo ;
2523
2624 public interface ScaleLayoutParams {
2725 ScaleLayoutInfo getScaleLayoutInfo ();
2826 }
2927
30- private ScaleLayoutHelper (ViewGroup mHost , float designWidth , float designHeight , float designDensity ) {
31- this .mHost = mHost ;
32- this .designWidth = designWidth ;
33- this .designHeight = designHeight ;
34- this .designDensity = designDensity ;
28+ public ScaleLayoutHelper (ViewGroup host , ScaleLayoutInfo layoutInfo ) {
29+ this .mHost = host ;
30+ this .mHostLayoutInfo = layoutInfo ;
3531 }
3632
3733 public static ScaleLayoutHelper create (ViewGroup view , AttributeSet attrs ) {
38- TypedArray array = view .getContext ().obtainStyledAttributes (attrs , R .styleable .ScaleLayout );
39- int width = 0 , height = 0 , density = 0 ;
40-
41- int value = array .getDimensionPixelSize (R .styleable .ScaleLayout_layout_design_width , 0 );
42- if (value != 0 ) {
43- width = value ;
44- }
45- value = array .getDimensionPixelSize (R .styleable .ScaleLayout_layout_design_height , 0 );
46- if (value != 0 ) {
47- height = value ;
48- }
49- value = array .getInteger (R .styleable .ScaleLayout_layout_design_density , 0 );
50- if (value != 0 ) {
51- density = value ;
52- }
53- array .recycle ();
54- return new ScaleLayoutHelper (view , width , height , density );
34+ return new ScaleLayoutHelper (view , getScaleLayoutInfo (view .getContext (), attrs ));
5535 }
5636
5737 public void adjustChildren (int widthMeasureSpec , int heightMeasureSpec ) {
@@ -66,8 +46,8 @@ public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
6646 if (params instanceof ScaleLayoutParams ) {
6747 ScaleLayoutInfo info = ((ScaleLayoutParams ) params ).getScaleLayoutInfo ();
6848 Log .d (TAG , "using " + info );
69- if (info != null ) {
70- info .setDesignSize (designWidth , designHeight , designDensity );
49+ if (info != null && mHostLayoutInfo != null ) {
50+ info .setDesignSize (mHostLayoutInfo . designWidth , mHostLayoutInfo . designHeight , mHostLayoutInfo . designDensity );
7151 if (params instanceof ViewGroup .MarginLayoutParams ) {
7252 info .fillMarginLayoutParams ((ViewGroup .MarginLayoutParams ) params );
7353 } else {
@@ -79,6 +59,18 @@ public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
7959 }
8060 }
8161
62+ public void adjustSelf (int widthMeasureSpec , int heightMeasureSpec ) {
63+ if (mHostLayoutInfo != null ) {
64+ ViewGroup .LayoutParams params = mHost .getLayoutParams ();
65+ if (params instanceof ViewGroup .MarginLayoutParams ) {
66+ mHostLayoutInfo .fillMarginLayoutParams ((ViewGroup .MarginLayoutParams ) params );
67+ } else {
68+ mHostLayoutInfo .fillLayoutParams (params );
69+ }
70+ mHost .setPadding (mHostLayoutInfo .leftPadding , mHostLayoutInfo .topPadding , mHostLayoutInfo .rightPadding , mHostLayoutInfo .bottomPadding );
71+ }
72+ }
73+
8274 public void restoreOriginalParams () {
8375 int i = 0 ;
8476
@@ -199,6 +191,24 @@ public static ScaleLayoutInfo getScaleLayoutInfo(Context context, AttributeSet a
199191 info .bottomPadding = value ;
200192 }
201193
194+ value = array .getDimensionPixelSize (R .styleable .ScaleLayout_layout_design_width , 0 );
195+ if (value != 0 ) {
196+ info = info != null ? info : new ScaleLayoutInfo (context );
197+ info .designWidth = value ;
198+ }
199+
200+ value = array .getDimensionPixelSize (R .styleable .ScaleLayout_layout_design_height , 0 );
201+ if (value != 0 ) {
202+ info = info != null ? info : new ScaleLayoutInfo (context );
203+ info .designHeight = value ;
204+ }
205+
206+ value = array .getInteger (R .styleable .ScaleLayout_layout_design_density , 0 );
207+ if (value != 0 ) {
208+ info = info != null ? info : new ScaleLayoutInfo (context );
209+ info .designDensity = value ;
210+ }
211+
202212 array .recycle ();
203213
204214 Log .d (TAG , "constructed: " + info );
@@ -285,13 +295,29 @@ public void fillLayoutParams(ViewGroup.LayoutParams params) {
285295 this .mPreservedParams .width = params .width ;
286296 this .mPreservedParams .height = params .height ;
287297
288- if (this .width > 0 ) {
298+ if (this .width > 0.0F ) {
289299 params .width = getRealPixelSize (this .width );
290300 }
291301
292- if (this .height > 0 ) {
302+ if (this .height > 0.0F ) {
293303 params .height = getRealPixelSize (this .height );
294304 }
305+
306+ if (this .leftPadding > 0.0F ) {
307+ this .leftPadding = getRealPixelSize (this .leftPadding );
308+ }
309+
310+ if (this .topPadding > 0.0F ) {
311+ this .topPadding = getRealPixelSize (this .topPadding );
312+ }
313+
314+ if (this .rightPadding > 0.0F ) {
315+ this .rightPadding = getRealPixelSize (this .rightPadding );
316+ }
317+
318+ if (this .bottomPadding > 0.0F ) {
319+ this .bottomPadding = getRealPixelSize (this .bottomPadding );
320+ }
295321 }
296322
297323 public void fillMarginLayoutParams (ViewGroup .MarginLayoutParams params ) {
@@ -303,27 +329,27 @@ public void fillMarginLayoutParams(ViewGroup.MarginLayoutParams params) {
303329 MarginLayoutParamsCompat .setMarginStart (this .mPreservedParams , MarginLayoutParamsCompat .getMarginStart (params ));
304330 MarginLayoutParamsCompat .setMarginEnd (this .mPreservedParams , MarginLayoutParamsCompat .getMarginEnd (params ));
305331
306- if (this .leftMargin >= 0.0F ) {
332+ if (this .leftMargin > 0.0F ) {
307333 params .leftMargin = getRealPixelSize (this .leftMargin );
308334 }
309335
310- if (this .topMargin >= 0.0F ) {
336+ if (this .topMargin > 0.0F ) {
311337 params .topMargin = getRealPixelSize (this .topMargin );
312338 }
313339
314- if (this .rightMargin >= 0.0F ) {
340+ if (this .rightMargin > 0.0F ) {
315341 params .rightMargin = getRealPixelSize (this .rightMargin );
316342 }
317343
318- if (this .bottomMargin >= 0.0F ) {
344+ if (this .bottomMargin > 0.0F ) {
319345 params .bottomMargin = getRealPixelSize (this .bottomMargin );
320346 }
321347
322- if (this .startMargin >= 0.0F ) {
348+ if (this .startMargin > 0.0F ) {
323349 MarginLayoutParamsCompat .setMarginStart (params , getRealPixelSize (this .startMargin ));
324350 }
325351
326- if (this .endMargin >= 0.0F ) {
352+ if (this .endMargin > 0.0F ) {
327353 MarginLayoutParamsCompat .setMarginEnd (params , getRealPixelSize (this .endMargin ));
328354 }
329355
0 commit comments