Skip to content

Commit

Permalink
修复了px转string造成的频繁警告 fixed #22
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyangAndroid committed Dec 23, 2015
1 parent 7e64194 commit 276fa91
Show file tree
Hide file tree
Showing 35 changed files with 719 additions and 23 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ dependencies {

```
dependencies {
compile 'com.zhy:autolayout:1.3.4'
compile 'com.zhy:autolayout:1.3.5'
}
```

Expand Down Expand Up @@ -97,6 +97,9 @@ dependencies {
这样也可以完成适配。





## 目前支持属性

* layout_width
Expand All @@ -106,6 +109,26 @@ dependencies {
* textSize


## 配置

默认使用的宽度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:

可以在Application的onCreate方法中进行设置:

```java
public class UseDeviceSizeApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
AutoLayoutConifg.getInstance().useDeviceSize();
}
}

```


## 预览

大家都知道,写布局文件的时候,不能实时的去预览效果,那么体验真的是非常的不好,也在很大程度上降低开发效率,所以下面教大家如何用好,用对PreView(针对该库)。
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.4"
version = "1.3.5"

android {
compileSdkVersion 23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public void apply(View view)

protected int getPercentWidthSize()
{
return AutoUtils.getPercentWidthSize(pxVal);
return AutoUtils.getPercentWidthSizeBigger(pxVal);
}

protected int getPercentHeightSize()
{
return AutoUtils.getPercentHeightSize(pxVal);
return AutoUtils.getPercentHeightSizeBigger(pxVal);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ public class AutoLayoutConifg
private int mDesignWidth;
private int mDesignHeight;

private boolean useDeviceSize;


private AutoLayoutConifg()
{
}

public void checkParams()
{
if(mDesignHeight <=0 || mDesignWidth <= 0 )
if (mDesignHeight <= 0 || mDesignWidth <= 0)
{
throw new RuntimeException(
"you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.");
}
}

public AutoLayoutConifg useDeviceSize()
{
useDeviceSize = true;
return this;
}


public static AutoLayoutConifg getInstance()
{
Expand Down Expand Up @@ -71,7 +79,7 @@ public void init(Context context)
{
getMetaData(context);

int[] screenSize = ScreenUtils.getScreenSize(context);
int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize);
mScreenWidth = screenSize[0];
mScreenHeight = screenSize[1];
L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

Expand Down Expand Up @@ -139,9 +140,10 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
for (int i = 0; i < n; i++)
{
int index = array.getIndex(i);
String val = array.getString(index);
// String val = array.getString(index);
// if (!isPxVal(val)) continue;

if (!isPxVal(val)) continue;
if (!isPxVal(array.peekValue(index))) continue;

int pxVal = 0;
try
Expand Down Expand Up @@ -200,6 +202,21 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
return info;
}

private static boolean isPxVal(TypedValue val)
{
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
{
return true;
}
return false;
}

private static int getComplexUnit(int data)
{
return TypedValue.COMPLEX_UNIT_MASK & (data >> TypedValue.COMPLEX_UNIT_SHIFT);
}

private static boolean isPxVal(String val)
{
if (val.endsWith("px"))
Expand Down
42 changes: 35 additions & 7 deletions autolayout/src/main/java/com/zhy/autolayout/utils/AutoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void autoMargin(View view)
return;

ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
if(lp == null)return ;
if (lp == null) return;

Object tag = view.getTag(R.id.id_tag_autolayout_margin);
if (tag != null) return;
Expand Down Expand Up @@ -73,23 +73,19 @@ public static void autoSize(View view)

view.setTag(R.id.id_tag_autolayout_size, "Just Identify");

if(lp.width>0)
if (lp.width > 0)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
}

if(lp.height>0)
if (lp.height > 0)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
}




}

public static int getPercentWidthSize(int val)
Expand All @@ -100,6 +96,38 @@ public static int getPercentWidthSize(int val)
return (int) (val * 1.0f / designWidth * screenWidth);
}


public static int getPercentWidthSizeBigger(int val)
{
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();

int res = val * screenWidth;
if (res % designWidth == 0)
{
return res / designWidth;
} else
{
return res / designWidth + 1;
}

}

public static int getPercentHeightSizeBigger(int val)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();

int res = val * screenHeight;
if (res % designHeight == 0)
{
return res / designHeight;
} else
{
return res / designHeight + 1;
}
}

public static int getPercentHeightSize(int val)
{
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*/
public class ScreenUtils
{
public static int[] getScreenSize(Context context)


public static int[] getScreenSize(Context context, boolean useDeviceSize)
{

int[] size = new int[2];
Expand All @@ -25,6 +27,14 @@ public static int[] getScreenSize(Context context)
// since SDK_INT = 1;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;

if (!useDeviceSize)
{
size[0] = widthPixels;
size[1] = heightPixels;
return size;
}

// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
try
Expand Down
Loading

0 comments on commit 276fa91

Please sign in to comment.