Skip to content

Commit 21b9e10

Browse files
committed
optimization code, use android: name space attr.
1 parent 40a28e1 commit 21b9e10

File tree

11 files changed

+368
-174
lines changed

11 files changed

+368
-174
lines changed

Library/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 22
5-
buildToolsVersion "22.0.1"
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.1"
66

77
defaultConfig {
8-
minSdkVersion 7
9-
targetSdkVersion 22
8+
minSdkVersion 9
9+
targetSdkVersion 24
1010
versionCode 1
1111
versionName "1.0"
1212
}
@@ -20,5 +20,5 @@ android {
2020

2121
dependencies {
2222
compile fileTree(dir: 'libs', include: ['*.jar'])
23-
compile 'com.android.support:support-v4:22.2.0'
23+
compile 'com.android.support:support-v4:24.2.0'
2424
}

Library/src/main/java/cn/gavinliu/android/lib/scale/ScaleFrameLayout.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ public ScaleFrameLayout(Context context, AttributeSet attrs, int defStyle) {
2828
}
2929

3030
@Override
31-
public ScaleFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
32-
return new ScaleFrameLayout.LayoutParams(this.getContext(), attrs);
31+
protected LayoutParams generateDefaultLayoutParams() {
32+
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
33+
}
34+
35+
@Override
36+
public LayoutParams generateLayoutParams(AttributeSet attrs) {
37+
return new LayoutParams(this.getContext(), attrs);
3338
}
3439

3540
@Override
3641
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
42+
this.mHelper.adjustHost(widthMeasureSpec, heightMeasureSpec);
3743
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
38-
this.mHelper.adjustSelf(widthMeasureSpec, heightMeasureSpec);
3944

4045
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
4146
if (this.mHelper.handleMeasuredStateTooSmall()) {
@@ -79,5 +84,12 @@ protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
7984
ScaleLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
8085
}
8186

87+
@Override
88+
public void setMargins(int left, int top, int right, int bottom) {
89+
super.setMargins(left, top, right, bottom);
90+
if (mPercentLayoutInfo != null) {
91+
mPercentLayoutInfo.setMargins(left, top, right, bottom);
92+
}
93+
}
8294
}
8395
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package cn.gavinliu.android.lib.scale;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.content.res.TypedArray;
6+
import android.os.Build;
7+
import android.util.AttributeSet;
8+
import android.widget.LinearLayout;
9+
10+
import cn.gavinliu.android.lib.scale.helper.ScaleLayoutHelper;
11+
12+
/**
13+
* Created by Gavin on 16-9-20.
14+
*/
15+
public class ScaleLinearLayout extends LinearLayout {
16+
17+
private ScaleLayoutHelper mHelper;
18+
19+
public ScaleLinearLayout(Context context) {
20+
this(context, null);
21+
}
22+
23+
public ScaleLinearLayout(Context context, AttributeSet attrs) {
24+
this(context, attrs, 0);
25+
}
26+
27+
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
28+
public ScaleLinearLayout(Context context, AttributeSet attrs, int defStyle) {
29+
super(context, attrs, defStyle);
30+
mHelper = ScaleLayoutHelper.create(this, attrs);
31+
}
32+
33+
@Override
34+
protected LayoutParams generateDefaultLayoutParams() {
35+
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
36+
}
37+
38+
@Override
39+
public LayoutParams generateLayoutParams(AttributeSet attrs) {
40+
return new LayoutParams(this.getContext(), attrs);
41+
}
42+
43+
@Override
44+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
45+
this.mHelper.adjustHost(widthMeasureSpec, heightMeasureSpec);
46+
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
47+
48+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49+
if (this.mHelper.handleMeasuredStateTooSmall()) {
50+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
51+
}
52+
}
53+
54+
@Override
55+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
56+
super.onLayout(changed, left, top, right, bottom);
57+
this.mHelper.restoreOriginalParams();
58+
}
59+
60+
public static class LayoutParams extends LinearLayout.LayoutParams implements ScaleLayoutHelper.ScaleLayoutParams {
61+
private ScaleLayoutHelper.ScaleLayoutInfo mPercentLayoutInfo;
62+
63+
public LayoutParams(Context c, AttributeSet attrs) {
64+
super(c, attrs);
65+
this.mPercentLayoutInfo = ScaleLayoutHelper.getScaleLayoutInfo(c, attrs);
66+
}
67+
68+
public LayoutParams(int width, int height) {
69+
super(width, height);
70+
}
71+
72+
public LayoutParams(android.view.ViewGroup.LayoutParams source) {
73+
super(source);
74+
}
75+
76+
public LayoutParams(MarginLayoutParams source) {
77+
super(source);
78+
}
79+
80+
@Override
81+
public ScaleLayoutHelper.ScaleLayoutInfo getScaleLayoutInfo() {
82+
return this.mPercentLayoutInfo;
83+
}
84+
85+
@Override
86+
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
87+
ScaleLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
88+
}
89+
90+
@Override
91+
public void setMargins(int left, int top, int right, int bottom) {
92+
super.setMargins(left, top, right, bottom);
93+
if (mPercentLayoutInfo != null) {
94+
mPercentLayoutInfo.setMargins(left, top, right, bottom);
95+
}
96+
}
97+
}
98+
}

Library/src/main/java/cn/gavinliu/android/lib/scale/ScaleRelativeLayout.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ public ScaleRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
2828
}
2929

3030
@Override
31-
public ScaleRelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
32-
return new ScaleRelativeLayout.LayoutParams(this.getContext(), attrs);
31+
protected LayoutParams generateDefaultLayoutParams() {
32+
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
33+
}
34+
35+
@Override
36+
public LayoutParams generateLayoutParams(AttributeSet attrs) {
37+
return new LayoutParams(this.getContext(), attrs);
3338
}
3439

3540
@Override
3641
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
42+
this.mHelper.adjustHost(widthMeasureSpec, heightMeasureSpec);
3743
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
38-
this.mHelper.adjustSelf(widthMeasureSpec, heightMeasureSpec);
39-
44+
4045
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
4146
if (this.mHelper.handleMeasuredStateTooSmall()) {
4247
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
@@ -79,5 +84,12 @@ protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
7984
ScaleLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
8085
}
8186

87+
@Override
88+
public void setMargins(int left, int top, int right, int bottom) {
89+
super.setMargins(left, top, right, bottom);
90+
if (mPercentLayoutInfo != null) {
91+
mPercentLayoutInfo.setMargins(left, top, right, bottom);
92+
}
93+
}
8294
}
8395
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cn.gavinliu.android.lib.scale.config;
2+
3+
import android.content.Context;
4+
import android.content.res.Configuration;
5+
6+
/**
7+
* Created by Gavin on 16-9-20.
8+
*/
9+
public class ScaleConfig {
10+
11+
private int mDesignWidth = 1080;
12+
private int mDesignHeight = 1920;
13+
14+
private int mScreenWidth;
15+
private int mScreenHeight;
16+
17+
private int mScreenWidthLand;
18+
private int mScreenHeightLand;
19+
20+
private static ScaleConfig mInstance;
21+
22+
public static void create(Context ctx) {
23+
if (mInstance == null) {
24+
mInstance = new ScaleConfig(ctx);
25+
}
26+
}
27+
28+
public static ScaleConfig getInstance() {
29+
return mInstance;
30+
}
31+
32+
private ScaleConfig(Context ctx) {
33+
final int width = ctx.getResources().getDisplayMetrics().widthPixels;
34+
final int height = ctx.getResources().getDisplayMetrics().heightPixels;
35+
36+
if (ctx.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
37+
mScreenWidth = width;
38+
mScreenHeight = height;
39+
40+
mScreenWidthLand = height;
41+
mScreenHeightLand = width;
42+
} else {
43+
mScreenWidth = height;
44+
mScreenHeight = width;
45+
46+
mScreenWidthLand = width;
47+
mScreenHeightLand = height;
48+
}
49+
50+
}
51+
52+
public int getDesignWidth() {
53+
return mDesignWidth;
54+
}
55+
56+
public int getDesignHeight() {
57+
return mDesignHeight;
58+
}
59+
60+
public int getScreenWidth() {
61+
return mScreenWidth;
62+
}
63+
64+
public int getScreenHeight() {
65+
return mScreenHeight;
66+
}
67+
}

0 commit comments

Comments
 (0)