Skip to content

Commit 276fa91

Browse files
author
hongyangAndroid
committed
修复了px转string造成的频繁警告 fixed #22
1 parent 7e64194 commit 276fa91

File tree

35 files changed

+719
-23
lines changed

35 files changed

+719
-23
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ dependencies {
5555

5656
```
5757
dependencies {
58-
compile 'com.zhy:autolayout:1.3.4'
58+
compile 'com.zhy:autolayout:1.3.5'
5959
}
6060
```
6161

@@ -97,6 +97,9 @@ dependencies {
9797
这样也可以完成适配。
9898

9999

100+
101+
102+
100103
## 目前支持属性
101104

102105
* layout_width
@@ -106,6 +109,26 @@ dependencies {
106109
* textSize
107110

108111

112+
## 配置
113+
114+
默认使用的宽度是设备的可用高度,也就是不包括状态栏和底部的操作栏的,如果你希望拿设备的物理高度进行百分比化:
115+
116+
可以在Application的onCreate方法中进行设置:
117+
118+
```java
119+
public class UseDeviceSizeApplication extends Application
120+
{
121+
@Override
122+
public void onCreate()
123+
{
124+
super.onCreate();
125+
AutoLayoutConifg.getInstance().useDeviceSize();
126+
}
127+
}
128+
129+
```
130+
131+
109132
## 预览
110133

111134
大家都知道,写布局文件的时候,不能实时的去预览效果,那么体验真的是非常的不好,也在很大程度上降低开发效率,所以下面教大家如何用好,用对PreView(针对该库)。

autolayout/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "1.3.4"
5+
version = "1.3.5"
66

77
android {
88
compileSdkVersion 23

autolayout/src/main/java/com/zhy/autolayout/attr/AutoAttr.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ public void apply(View view)
6161

6262
protected int getPercentWidthSize()
6363
{
64-
return AutoUtils.getPercentWidthSize(pxVal);
64+
return AutoUtils.getPercentWidthSizeBigger(pxVal);
6565
}
6666

6767
protected int getPercentHeightSize()
6868
{
69-
return AutoUtils.getPercentHeightSize(pxVal);
69+
return AutoUtils.getPercentHeightSizeBigger(pxVal);
7070
}
7171

7272

autolayout/src/main/java/com/zhy/autolayout/config/AutoLayoutConifg.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ public class AutoLayoutConifg
2525
private int mDesignWidth;
2626
private int mDesignHeight;
2727

28+
private boolean useDeviceSize;
29+
2830

2931
private AutoLayoutConifg()
3032
{
3133
}
3234

3335
public void checkParams()
3436
{
35-
if(mDesignHeight <=0 || mDesignWidth <= 0 )
37+
if (mDesignHeight <= 0 || mDesignWidth <= 0)
3638
{
3739
throw new RuntimeException(
3840
"you must set " + KEY_DESIGN_WIDTH + " and " + KEY_DESIGN_HEIGHT + " in your manifest file.");
3941
}
4042
}
4143

44+
public AutoLayoutConifg useDeviceSize()
45+
{
46+
useDeviceSize = true;
47+
return this;
48+
}
49+
4250

4351
public static AutoLayoutConifg getInstance()
4452
{
@@ -71,7 +79,7 @@ public void init(Context context)
7179
{
7280
getMetaData(context);
7381

74-
int[] screenSize = ScreenUtils.getScreenSize(context);
82+
int[] screenSize = ScreenUtils.getScreenSize(context, useDeviceSize);
7583
mScreenWidth = screenSize[0];
7684
mScreenHeight = screenSize[1];
7785
L.e(" screenWidth =" + mScreenWidth + " ,screenHeight = " + mScreenHeight);

autolayout/src/main/java/com/zhy/autolayout/utils/AutoLayoutHelper.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.content.Context;
2020
import android.content.res.TypedArray;
2121
import android.util.AttributeSet;
22+
import android.util.TypedValue;
2223
import android.view.View;
2324
import android.view.ViewGroup;
2425

@@ -139,9 +140,10 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
139140
for (int i = 0; i < n; i++)
140141
{
141142
int index = array.getIndex(i);
142-
String val = array.getString(index);
143+
// String val = array.getString(index);
144+
// if (!isPxVal(val)) continue;
143145

144-
if (!isPxVal(val)) continue;
146+
if (!isPxVal(array.peekValue(index))) continue;
145147

146148
int pxVal = 0;
147149
try
@@ -200,6 +202,21 @@ public static AutoLayoutInfo getAutoLayoutInfo(Context context,
200202
return info;
201203
}
202204

205+
private static boolean isPxVal(TypedValue val)
206+
{
207+
if (val != null && val.type == TypedValue.TYPE_DIMENSION &&
208+
getComplexUnit(val.data) == TypedValue.COMPLEX_UNIT_PX)
209+
{
210+
return true;
211+
}
212+
return false;
213+
}
214+
215+
private static int getComplexUnit(int data)
216+
{
217+
return TypedValue.COMPLEX_UNIT_MASK & (data >> TypedValue.COMPLEX_UNIT_SHIFT);
218+
}
219+
203220
private static boolean isPxVal(String val)
204221
{
205222
if (val.endsWith("px"))

autolayout/src/main/java/com/zhy/autolayout/utils/AutoUtils.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void autoMargin(View view)
3030
return;
3131

3232
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
33-
if(lp == null)return ;
33+
if (lp == null) return;
3434

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

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

76-
if(lp.width>0)
76+
if (lp.width > 0)
7777
{
7878
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
7979
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
8080
lp.width = (int) (lp.width * 1.0f / designWidth * screenWidth);
8181
}
8282

83-
if(lp.height>0)
83+
if (lp.height > 0)
8484
{
8585
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
8686
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
8787
lp.height = (int) (lp.height * 1.0f / designHeight * screenHeight);
8888
}
89-
90-
91-
92-
9389
}
9490

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

99+
100+
public static int getPercentWidthSizeBigger(int val)
101+
{
102+
int screenWidth = AutoLayoutConifg.getInstance().getScreenWidth();
103+
int designWidth = AutoLayoutConifg.getInstance().getDesignWidth();
104+
105+
int res = val * screenWidth;
106+
if (res % designWidth == 0)
107+
{
108+
return res / designWidth;
109+
} else
110+
{
111+
return res / designWidth + 1;
112+
}
113+
114+
}
115+
116+
public static int getPercentHeightSizeBigger(int val)
117+
{
118+
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();
119+
int designHeight = AutoLayoutConifg.getInstance().getDesignHeight();
120+
121+
int res = val * screenHeight;
122+
if (res % designHeight == 0)
123+
{
124+
return res / designHeight;
125+
} else
126+
{
127+
return res / designHeight + 1;
128+
}
129+
}
130+
103131
public static int getPercentHeightSize(int val)
104132
{
105133
int screenHeight = AutoLayoutConifg.getInstance().getScreenHeight();

autolayout/src/main/java/com/zhy/autolayout/utils/ScreenUtils.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*/
1414
public class ScreenUtils
1515
{
16-
public static int[] getScreenSize(Context context)
16+
17+
18+
public static int[] getScreenSize(Context context, boolean useDeviceSize)
1719
{
1820

1921
int[] size = new int[2];
@@ -25,6 +27,14 @@ public static int[] getScreenSize(Context context)
2527
// since SDK_INT = 1;
2628
int widthPixels = metrics.widthPixels;
2729
int heightPixels = metrics.heightPixels;
30+
31+
if (!useDeviceSize)
32+
{
33+
size[0] = widthPixels;
34+
size[1] = heightPixels;
35+
return size;
36+
}
37+
2838
// includes window decorations (statusbar bar/menu bar)
2939
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
3040
try

0 commit comments

Comments
 (0)