Skip to content

Commit 9e8ea30

Browse files
authored
Merge pull request #5 from gavinliu/dev
add class ScaleConfig
2 parents 21b9e10 + db2dc77 commit 9e8ea30

File tree

9 files changed

+95
-55
lines changed

9 files changed

+95
-55
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
4343
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
4444

4545
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
46-
if (this.mHelper.handleMeasuredStateTooSmall()) {
47-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
48-
}
46+
// if (this.mHelper.handleMeasuredStateTooSmall()) {
47+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
48+
// }
4949
}
5050

5151
@Override
5252
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
5353
super.onLayout(changed, left, top, right, bottom);
54-
this.mHelper.restoreOriginalParams();
54+
// this.mHelper.restoreOriginalParams();
5555
}
5656

5757
public static class LayoutParams extends FrameLayout.LayoutParams implements ScaleLayoutHelper.ScaleLayoutParams {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
4646
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
4747

4848
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49-
if (this.mHelper.handleMeasuredStateTooSmall()) {
50-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
51-
}
49+
// if (this.mHelper.handleMeasuredStateTooSmall()) {
50+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
51+
// }
5252
}
5353

5454
@Override
5555
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
5656
super.onLayout(changed, left, top, right, bottom);
57-
this.mHelper.restoreOriginalParams();
57+
// this.mHelper.restoreOriginalParams();
5858
}
5959

6060
public static class LayoutParams extends LinearLayout.LayoutParams implements ScaleLayoutHelper.ScaleLayoutParams {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
4343
this.mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
4444

4545
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
46-
if (this.mHelper.handleMeasuredStateTooSmall()) {
47-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
48-
}
46+
// if (this.mHelper.handleMeasuredStateTooSmall()) {
47+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
48+
// }
4949
}
5050

5151
@Override
5252
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
5353
super.onLayout(changed, left, top, right, bottom);
54-
this.mHelper.restoreOriginalParams();
54+
// this.mHelper.restoreOriginalParams();
5555
}
5656

5757
public static class LayoutParams extends android.widget.RelativeLayout.LayoutParams implements ScaleLayoutHelper.ScaleLayoutParams {

Library/src/main/java/cn/gavinliu/android/lib/scale/config/ScaleConfig.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
package cn.gavinliu.android.lib.scale.config;
22

33
import android.content.Context;
4-
import android.content.res.Configuration;
54

65
/**
76
* Created by Gavin on 16-9-20.
87
*/
98
public class ScaleConfig {
109

11-
private int mDesignWidth = 1080;
12-
private int mDesignHeight = 1920;
10+
private int mDesignWidth;
11+
private int mDesignHeight;
12+
private float mDesignDensity;
1313

1414
private int mScreenWidth;
1515
private int mScreenHeight;
16-
17-
private int mScreenWidthLand;
18-
private int mScreenHeightLand;
16+
private float mScreenDensity;
1917

2018
private static ScaleConfig mInstance;
2119

22-
public static void create(Context ctx) {
20+
public static ScaleConfig create(Context ctx, int designWidth, int designHeight, int designDensity) {
2321
if (mInstance == null) {
24-
mInstance = new ScaleConfig(ctx);
22+
mInstance = new ScaleConfig(ctx, designWidth, designHeight, designDensity);
2523
}
24+
25+
return mInstance;
2626
}
2727

2828
public static ScaleConfig getInstance() {
29+
if (mInstance == null)
30+
throw new IllegalArgumentException("You must call ScaleConfig.create first");
31+
2932
return mInstance;
3033
}
3134

3235
private ScaleConfig(Context ctx) {
3336
final int width = ctx.getResources().getDisplayMetrics().widthPixels;
3437
final int height = ctx.getResources().getDisplayMetrics().heightPixels;
38+
final float density = ctx.getResources().getDisplayMetrics().density;
3539

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-
}
40+
mScreenWidth = width;
41+
mScreenHeight = height;
42+
mScreenDensity = density;
43+
}
4944

45+
private ScaleConfig(Context ctx, int designWidth, int designHeight, int designDensity) {
46+
this(ctx);
47+
mDesignWidth = designWidth;
48+
mDesignHeight = designHeight;
49+
mDesignDensity = designDensity;
5050
}
5151

5252
public int getDesignWidth() {
@@ -57,6 +57,14 @@ public int getDesignHeight() {
5757
return mDesignHeight;
5858
}
5959

60+
public float getDesignDensity() {
61+
return mDesignDensity;
62+
}
63+
64+
public float getScreenDensity() {
65+
return mScreenDensity;
66+
}
67+
6068
public int getScreenWidth() {
6169
return mScreenWidth;
6270
}

Library/src/main/java/cn/gavinliu/android/lib/scale/helper/ScaleLayoutHelper.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,10 @@ private int getRealPixelSize(int pix) {
378378
design = designWidth;
379379
break;
380380
}
381-
return getRealPixelSize(pix, screen, design);
381+
return getRealPixelSizeByDip(pix, screen, design);
382382
}
383383

384-
private int getRealPixelSize(int pix, int screen, int design) {
384+
private int getRealPixelSizeByPix(int pix, int screen, int design) {
385385
int result;
386386

387387
int res = pix * screen;
@@ -395,6 +395,21 @@ private int getRealPixelSize(int pix, int screen, int design) {
395395
Log.i(TAG, "pix:" + pix + ",result:" + result);
396396
return result;
397397
}
398+
399+
private int getRealPixelSizeByDip(int pix, int screen, int design) {
400+
float density = ScaleConfig.getInstance().getScreenDensity();
401+
float designDensity = ScaleConfig.getInstance().getDesignDensity();
402+
403+
float newpix = (screen * designDensity * pix) / (design * density);
404+
int result;
405+
if (newpix > 1) {
406+
result = (int) Math.rint((double) newpix);
407+
} else {
408+
result = (int) Math.ceil((double) newpix);
409+
}
410+
Log.i(TAG, "pix:" + pix + ",newPix:" + newpix + ",result:" + result);
411+
return result;
412+
}
398413
}
399414

400415
}

Sample/src/main/AndroidManifest.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="cn.gavinliu.android_scalelayout">
3+
package="cn.gavinliu.android_scalelayout">
44

55
<application
6+
android:name=".MainApplication"
67
android:allowBackup="true"
78
android:configChanges="keyboardHidden|orientation|screenSize|locale"
89
android:icon="@mipmap/ic_launcher"
@@ -12,9 +13,9 @@
1213
android:name=".MainActivity"
1314
android:label="@string/app_name">
1415
<intent-filter>
15-
<action android:name="android.intent.action.MAIN" />
16+
<action android:name="android.intent.action.MAIN"/>
1617

17-
<category android:name="android.intent.category.LAUNCHER" />
18+
<category android:name="android.intent.category.LAUNCHER"/>
1819
</intent-filter>
1920
</activity>
2021
</application>
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package cn.gavinliu.android_scalelayout;
22

3+
import android.content.Context;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
5-
6-
import cn.gavinliu.android.lib.scale.config.ScaleConfig;
6+
import android.util.AttributeSet;
7+
import android.view.View;
78

89
public class MainActivity extends AppCompatActivity {
910

1011
@Override
1112
protected void onCreate(Bundle savedInstanceState) {
1213
super.onCreate(savedInstanceState);
13-
ScaleConfig.create(getApplication());
1414
setContentView(R.layout.activity_main);
1515
}
1616

17-
// @Override
18-
// public View onCreateView(String name, Context context, AttributeSet attrs) {
17+
@Override
18+
public View onCreateView(String name, Context context, AttributeSet attrs) {
1919
// switch (name) {
2020
// case "FrameLayout":
2121
// return new ScaleFrameLayout(context, attrs);
@@ -24,6 +24,6 @@ protected void onCreate(Bundle savedInstanceState) {
2424
// case "RelativeLayout":
2525
// return new ScaleRelativeLayout(context, attrs);
2626
// }
27-
// return super.onCreateView(name, context, attrs);
28-
// }
27+
return super.onCreateView(name, context, attrs);
28+
}
2929
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.gavinliu.android_scalelayout;
2+
3+
import android.app.Application;
4+
5+
import cn.gavinliu.android.lib.scale.config.ScaleConfig;
6+
7+
/**
8+
* Created by Gavin on 16-11-10.
9+
*/
10+
11+
public class MainApplication extends Application {
12+
13+
@Override
14+
public void onCreate() {
15+
super.onCreate();
16+
ScaleConfig.create(this, 1080, 1920, 3);
17+
}
18+
}
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<cn.gavinliu.android.lib.scale.ScaleRelativeLayout
1+
<RelativeLayout
22
xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
android:id="@+id/container"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent">
@@ -14,14 +13,14 @@
1413
android:layout_marginTop="8dp"
1514
android:background="#46b34b"
1615
android:paddingLeft="10dp"
17-
android:text="xxxx"
18-
app:layout_scale_by="width"/>
16+
android:paddingTop="10dp"
17+
android:text="xxxx"/>
1918

20-
<cn.gavinliu.android.lib.scale.ScaleFrameLayout
19+
<FrameLayout
2120
android:layout_width="300dp"
2221
android:layout_height="300dp"
2322
android:layout_below="@id/view"
24-
android:layout_marginTop="5dp"
23+
android:layout_marginTop="8dp"
2524
android:background="#46b"
2625
android:paddingTop="8dp">
2726

@@ -30,10 +29,9 @@
3029
android:layout_height="120dp"
3130
android:layout_marginLeft="12dp"
3231
android:layout_marginTop="8dp"
33-
android:background="#46b34b"
34-
app:layout_scale_by="width"/>
32+
android:background="#46b34b"/>
3533

36-
</cn.gavinliu.android.lib.scale.ScaleFrameLayout>
34+
</FrameLayout>
3735

3836

39-
</cn.gavinliu.android.lib.scale.ScaleRelativeLayout>
37+
</RelativeLayout>

0 commit comments

Comments
 (0)