Skip to content

Commit

Permalink
增加autoutils 部分辅助方法
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Mar 4, 2016
1 parent 640d6a2 commit 629ce2d
Show file tree
Hide file tree
Showing 29 changed files with 611 additions and 106 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {

```
dependencies {
compile 'com.zhy:autolayout:1.3.8'
compile 'com.zhy:autolayout:1.4.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion autolayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.3.9"
version = "1.4.1"

android {
compileSdkVersion 23
Expand Down
117 changes: 117 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/AutoLayoutInfo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package com.zhy.autolayout;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhy.autolayout.attr.Attrs;
import com.zhy.autolayout.attr.AutoAttr;
import com.zhy.autolayout.attr.HeightAttr;
import com.zhy.autolayout.attr.MarginBottomAttr;
import com.zhy.autolayout.attr.MarginLeftAttr;
import com.zhy.autolayout.attr.MarginRightAttr;
import com.zhy.autolayout.attr.MarginTopAttr;
import com.zhy.autolayout.attr.MaxHeightAttr;
import com.zhy.autolayout.attr.MaxWidthAttr;
import com.zhy.autolayout.attr.MinHeightAttr;
import com.zhy.autolayout.attr.MinWidthAttr;
import com.zhy.autolayout.attr.TextSizeAttr;
import com.zhy.autolayout.attr.WidthAttr;

import java.util.ArrayList;
import java.util.List;

public class AutoLayoutInfo
{
private List<AutoAttr> autoAttrs = new ArrayList<>();

public void addAttr(AutoAttr autoAttr)
{
autoAttrs.add(autoAttr);
Expand All @@ -24,6 +39,108 @@ public void fillAttrs(View view)
}
}


public static AutoLayoutInfo getAttrFromView(View view, int attrs, int base)
{
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params == null) return null;
AutoLayoutInfo autoLayoutInfo = new AutoLayoutInfo();

// width & height
if ((attrs & Attrs.WIDTH) != 0 && params.width > 0)
{
autoLayoutInfo.addAttr(WidthAttr.generate(params.width, base));
}

if ((attrs & Attrs.HEIGHT) != 0 && params.height > 0)
{
autoLayoutInfo.addAttr(HeightAttr.generate(params.height, base));
}

//margin
if (params instanceof ViewGroup.MarginLayoutParams)
{
if ((attrs & Attrs.MARGIN) != 0)
{
autoLayoutInfo.addAttr(MarginLeftAttr.generate(((ViewGroup.MarginLayoutParams) params).leftMargin, base));
autoLayoutInfo.addAttr(MarginTopAttr.generate(((ViewGroup.MarginLayoutParams) params).topMargin, base));
autoLayoutInfo.addAttr(MarginRightAttr.generate(((ViewGroup.MarginLayoutParams) params).rightMargin, base));
autoLayoutInfo.addAttr(MarginBottomAttr.generate(((ViewGroup.MarginLayoutParams) params).bottomMargin, base));
}
if ((attrs & Attrs.MARGIN_LEFT) != 0)
{
autoLayoutInfo.addAttr(MarginLeftAttr.generate(((ViewGroup.MarginLayoutParams) params).leftMargin, base));
}
if ((attrs & Attrs.MARGIN_TOP) != 0)
{
autoLayoutInfo.addAttr(MarginTopAttr.generate(((ViewGroup.MarginLayoutParams) params).topMargin, base));
}
if ((attrs & Attrs.MARGIN_RIGHT) != 0)
{
autoLayoutInfo.addAttr(MarginRightAttr.generate(((ViewGroup.MarginLayoutParams) params).rightMargin, base));
}
if ((attrs & Attrs.MARGIN_BOTTOM) != 0)
{
autoLayoutInfo.addAttr(MarginBottomAttr.generate(((ViewGroup.MarginLayoutParams) params).bottomMargin, base));
}
}

//padding
if ((attrs & Attrs.PADDING) != 0)
{
autoLayoutInfo.addAttr(MarginLeftAttr.generate(view.getPaddingLeft(), base));
autoLayoutInfo.addAttr(MarginTopAttr.generate(view.getPaddingTop(), base));
autoLayoutInfo.addAttr(MarginRightAttr.generate(view.getPaddingRight(), base));
autoLayoutInfo.addAttr(MarginBottomAttr.generate(view.getPaddingBottom(), base));
}
if ((attrs & Attrs.PADDING_LEFT) != 0)
{
autoLayoutInfo.addAttr(MarginLeftAttr.generate(view.getPaddingLeft(), base));
}
if ((attrs & Attrs.PADDING_TOP) != 0)
{
autoLayoutInfo.addAttr(MarginTopAttr.generate(view.getPaddingTop(), base));
}
if ((attrs & Attrs.PADDING_RIGHT) != 0)
{
autoLayoutInfo.addAttr(MarginRightAttr.generate(view.getPaddingRight(), base));
}
if ((attrs & Attrs.PADDING_BOTTOM) != 0)
{
autoLayoutInfo.addAttr(MarginBottomAttr.generate(view.getPaddingBottom(), base));
}

//minWidth ,maxWidth , minHeight , maxHeight
if ((attrs & Attrs.MIN_WIDTH) != 0)
{
autoLayoutInfo.addAttr(MinWidthAttr.generate(MinWidthAttr.getMinWidth(view), base));
}
if ((attrs & Attrs.MAX_WIDTH) != 0)
{
autoLayoutInfo.addAttr(MaxWidthAttr.generate(MaxWidthAttr.getMaxWidth(view), base));
}
if ((attrs & Attrs.MIN_HEIGHT) != 0)
{
autoLayoutInfo.addAttr(MinHeightAttr.generate(MinHeightAttr.getMinHeight(view), base));
}
if ((attrs & Attrs.MAX_HEIGHT) != 0)
{
autoLayoutInfo.addAttr(MaxHeightAttr.generate(MaxHeightAttr.getMaxHeight(view), base));
}

//textsize

if (view instanceof TextView)
{
if ((attrs & Attrs.TEXTSIZE) != 0)
{
autoLayoutInfo.addAttr(TextSizeAttr.generate((int) ((TextView) view).getTextSize(), base));
}
}
return autoLayoutInfo;
}


@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
public abstract class AutoAttr
{
public static final int BASE_WIDTH = 1;
public static final int BASE_HEIGHT = 2;
public static final int BASE_DEFAULT = 3;

protected int pxVal;
protected int baseWidth;
protected int baseHeight;
Expand Down Expand Up @@ -109,6 +113,7 @@ protected boolean contains(int baseVal, int flag)

protected abstract void execute(View view, int val);


@Override
public String toString()
{
Expand Down
20 changes: 20 additions & 0 deletions autolayout/src/main/java/com/zhy/autolayout/attr/HeightAttr.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@ protected void execute(View view, int val)
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.height = val;
}

public static HeightAttr generate(int val, int baseFlag)
{
HeightAttr heightAttr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
heightAttr = new HeightAttr(val, Attrs.HEIGHT, 0);
break;
case AutoAttr.BASE_HEIGHT:
heightAttr = new HeightAttr(val, 0, Attrs.HEIGHT);
break;
case AutoAttr.BASE_DEFAULT:
heightAttr = new HeightAttr(val, 0, 0);
break;
}
return heightAttr;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ protected void execute(View view, int val)
{
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.leftMargin = lp.rightMargin = lp.topMargin = lp.bottomMargin = val;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@ protected void execute(View view, int val)
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.bottomMargin = val;
}

public static MarginBottomAttr generate(int val, int baseFlag)
{
MarginBottomAttr attr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
attr = new MarginBottomAttr(val, Attrs.MARGIN_BOTTOM, 0);
break;
case AutoAttr.BASE_HEIGHT:
attr = new MarginBottomAttr(val, 0, Attrs.MARGIN_BOTTOM);
break;
case AutoAttr.BASE_DEFAULT:
attr = new MarginBottomAttr(val, 0, 0);
break;
}
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,29 @@ protected boolean defaultBaseWidth()
@Override
protected void execute(View view, int val)
{
if(!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
{
return ;
return;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.leftMargin = val;
}

public static MarginLeftAttr generate(int val, int baseFlag)
{
MarginLeftAttr attr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
attr = new MarginLeftAttr(val, Attrs.MARGIN_LEFT, 0);
break;
case AutoAttr.BASE_HEIGHT:
attr = new MarginLeftAttr(val, 0, Attrs.MARGIN_LEFT);
break;
case AutoAttr.BASE_DEFAULT:
attr = new MarginLeftAttr(val, 0, 0);
break;
}
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ protected boolean defaultBaseWidth()
@Override
protected void execute(View view, int val)
{
if(!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
{
return ;
return;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.rightMargin = val;
}


public static MarginRightAttr generate(int val, int baseFlag)
{
MarginRightAttr attr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
attr = new MarginRightAttr(val, Attrs.MARGIN_RIGHT, 0);
break;
case AutoAttr.BASE_HEIGHT:
attr = new MarginRightAttr(val, 0, Attrs.MARGIN_RIGHT);
break;
case AutoAttr.BASE_DEFAULT:
attr = new MarginRightAttr(val, 0, 0);
break;
}
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ protected boolean defaultBaseWidth()
@Override
protected void execute(View view, int val)
{
if(!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
if (!(view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams))
{
return ;
return;
}
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
lp.topMargin = val;
}


public static MarginTopAttr generate(int val, int baseFlag)
{
MarginTopAttr attr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
attr = new MarginTopAttr(val, Attrs.MARGIN_TOP, 0);
break;
case AutoAttr.BASE_HEIGHT:
attr = new MarginTopAttr(val, 0, Attrs.MARGIN_TOP);
break;
case AutoAttr.BASE_DEFAULT:
attr = new MarginTopAttr(val, 0, 0);
break;
}
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,34 @@ protected void execute(View view, int val)
{
}
}

public static MaxHeightAttr generate(int val, int baseFlag)
{
MaxHeightAttr attr = null;
switch (baseFlag)
{
case AutoAttr.BASE_WIDTH:
attr = new MaxHeightAttr(val, Attrs.MAX_HEIGHT, 0);
break;
case AutoAttr.BASE_HEIGHT:
attr = new MaxHeightAttr(val, 0, Attrs.MAX_HEIGHT);
break;
case AutoAttr.BASE_DEFAULT:
attr = new MaxHeightAttr(val, 0, 0);
break;
}
return attr;
}

public static int getMaxHeight(View view)
{
try
{
Method setMaxWidthMethod = view.getClass().getMethod("getMaxHeight");
return (int) setMaxWidthMethod.invoke(view);
} catch (Exception ignore)
{
}
return 0;
}
}
Loading

0 comments on commit 629ce2d

Please sign in to comment.