Skip to content

Commit 1ae0bdd

Browse files
authored
Merge pull request #6 from gavinliu/dev
update version 1.0.1
2 parents 9e8ea30 + ad6b223 commit 1ae0bdd

File tree

28 files changed

+440
-187
lines changed

28 files changed

+440
-187
lines changed

Library/build.gradle

Lines changed: 0 additions & 24 deletions
This file was deleted.

Library/src/main/res/values/strings.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,121 +24,87 @@
2424
![screenhot](/screenhot.png)
2525

2626

27-
## 如何使用
27+
## 基本原理
2828

29-
### 基本原理
3029
+ 设计属性
31-
设计手机的宽高,像素密度,设计尺寸(dp)
32-
+ 设备属性
30+
设计手机的宽高,像素密度,设计尺寸
31+
+ 设备属性
3332
设备手机的宽高,像素密度
3433

3534
```java
3635
float realPixel = percent * designPixel
36+
```
37+
38+
### Pix 模式
39+
40+
```java
41+
float realPixel = percent * designPixel
42+
43+
float percent = mScreenWidth / designScreenWidth
44+
45+
float designPixel = res.getDimensionPixelSize()
46+
```
47+
```java
48+
float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth
49+
```
50+
51+
### DP 模式
52+
```java
53+
float realPixel = percent * designPixel
3754

3855
float percent = mScreenWidth / designScreenWidth // 屏幕比
39-
float designDP = getPixelSize() / mDensity // DesignDP是写入在xml文件中的,需要通过 getDimensionPixelSize() 还原出来
4056
float designPixel = designDP * designDensity // dp 转 pixel
57+
58+
float designDP = res.getDimensionPixelSize() / mDensity // DesignDP是写入在xml文件中的,需要通过 pix/density 还原出来
4159
```
4260
```java
4361
float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity)
4462
```
4563

46-
### 属性
64+
## 如何使用
65+
66+
### 支持的属性
4767

4868
```xml
49-
<?xml version="1.0" encoding="utf-8"?>
50-
<resources>
51-
<declare-styleable name="ScaleLayout">
52-
53-
<!-- 根据宽或者高缩放 -->
54-
<attr name="layout_scale_by" format="enum">
55-
<enum name="width" value="0" />
56-
<enum name="height" value="1" />
57-
</attr>
58-
59-
<!-- 设计图的相关属性:必须写在 Scale*Layout 中 -->
60-
<attr name="layout_design_width" format="integer" /> <!-- eg:1080 -->
61-
<attr name="layout_design_height" format="integer" /> <!-- eg:1920 -->
62-
<attr name="layout_design_density" format="integer" /> <!-- eg:3 -->
63-
64-
<!-- 控件大小间距相关属性:下面的单位都应该写 dp -->
65-
<attr name="layout_width" format="dimension" />
66-
<attr name="layout_height" format="dimension" />
67-
68-
<attr name="layout_margin" format="dimension" />
69-
<attr name="layout_marginLeft" format="dimension" />
70-
<attr name="layout_marginTop" format="dimension" />
71-
<attr name="layout_marginRight" format="dimension" />
72-
<attr name="layout_marginBottom" format="dimension" />
73-
<attr name="layout_marginStart" format="dimension" />
74-
<attr name="layout_marginEnd" format="dimension" />
75-
76-
<attr name="layout_paddingLeft" format="dimension" />
77-
<attr name="layout_paddingTop" format="dimension" />
78-
<attr name="layout_paddingRight" format="dimension" />
79-
<attr name="layout_paddingBottom" format="dimension" />
80-
</declare-styleable>
81-
82-
</resources>
69+
<attr name="android:layout_width"/>
70+
<attr name="android:layout_height"/>
71+
72+
<attr name="android:layout_margin"/>
73+
<attr name="android:layout_marginLeft"/>
74+
<attr name="android:layout_marginTop"/>
75+
<attr name="android:layout_marginRight"/>
76+
<attr name="android:layout_marginBottom"/>
77+
<attr name="android:layout_marginStart"/>
78+
<attr name="android:layout_marginEnd"/>
79+
80+
<attr name="android:padding"/>
81+
<attr name="android:paddingLeft"/>
82+
<attr name="android:paddingTop"/>
83+
<attr name="android:paddingRight"/>
84+
<attr name="android:paddingBottom"/>
85+
<attr name="android:paddingStart"/>
86+
<attr name="android:paddingEnd"/>
8387
```
8488

85-
### 使用
89+
### 初始化
8690

87-
```xml
88-
<cn.gavinliu.android.lib.scale.ScaleRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
89-
xmlns:app="http://schemas.android.com/apk/res-auto"
90-
android:layout_width="match_parent"
91-
android:layout_height="match_parent"
92-
app:layout_design_density="@integer/app_design_density"
93-
app:layout_design_height="@dimen/app_design_height"
94-
app:layout_design_width="@dimen/app_design_width">
95-
96-
<TextView
97-
android:id="@+id/view"
98-
android:layout_width="wrap_content"
99-
android:layout_height="wrap_content"
100-
android:background="#46b34b"
101-
app:layout_height="120dp"
102-
app:layout_marginLeft="12dp"
103-
app:layout_marginTop="8dp"
104-
app:layout_scale_by="width"
105-
app:layout_width="166dp" />
106-
107-
<cn.gavinliu.android.lib.scale.ScaleRelativeLayout
108-
android:layout_width="wrap_content"
109-
android:layout_height="wrap_content"
110-
android:layout_below="@id/view"
111-
android:background="#46b"
112-
app:layout_design_density="@integer/app_design_density"
113-
app:layout_design_height="@dimen/app_design_height"
114-
app:layout_design_width="@dimen/app_design_width"
115-
app:layout_height="300dp"
116-
app:layout_marginTop="5dp"
117-
app:layout_width="300dp">
118-
119-
<TextView
120-
android:layout_width="wrap_content"
121-
android:layout_height="wrap_content"
122-
android:background="#46b34b"
123-
app:layout_height="120dp"
124-
app:layout_marginLeft="12dp"
125-
app:layout_marginTop="8dp"
126-
app:layout_scale_by="width"
127-
app:layout_width="166dp" />
128-
129-
</cn.gavinliu.android.lib.scale.ScaleRelativeLayout>
130-
131-
</cn.gavinliu.android.lib.scale.ScaleRelativeLayout>
91+
```java
92+
ScaleConfig.create(this,
93+
1080, // 设计宽
94+
1920, // 设计高
95+
3, // 设计像素密度
96+
ScaleConfig.DIMENS_UNIT_DP); // 填入的是 dp or pix
13297
```
13398

99+
### 接入
100+
101+
只需要把 FrameLayout,LinearLayout,RelativeLayout 换成 ScaleFrameLayout,ScaleLinearLayout,ScaleRelativeLayout 即可。
102+
134103
### 注意事项
135104

136-
1. 建议缩放方式
105+
1. 建议缩放方式
137106
上下滑动的界面按**屏幕宽**等比缩放
138-
左右滑动的界面按**屏幕高**等比缩放
139-
2. 控件须知
140-
``cn.gavinliu.android.lib.scale.Scale*Layout`` 必须包含 layout_design_*
141-
``layout_scale_by`` 默认值为 width
107+
左右滑动的界面按**屏幕高**等比缩放
142108

143109
## License
144110

Sample/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ android {
2020
}
2121

2222
dependencies {
23-
compile project(':Library')
23+
compile project(':ScaleLayout')
24+
// compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.1'
25+
2426
compile 'com.android.support:appcompat-v7:24.2.0'
25-
compile 'com.android.support:percent:24.2.0'
2627
}

Sample/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
<category android:name="android.intent.category.LAUNCHER"/>
1919
</intent-filter>
2020
</activity>
21+
22+
<activity
23+
android:name=".DemoActivity"
24+
android:label="Demo"/>
25+
26+
<activity
27+
android:name=".DemoListActivity"
28+
android:label="Demo List"/>
29+
2130
</application>
2231

2332
</manifest>
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.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v7.app.AppCompatActivity;
6+
7+
/**
8+
* Created by Gavin on 2016/11/20.
9+
*/
10+
11+
public class DemoActivity extends AppCompatActivity {
12+
13+
@Override
14+
protected void onCreate(@Nullable Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
setContentView(R.layout.activity_demo);
17+
}
18+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cn.gavinliu.android_scalelayout;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.view.LayoutInflater;
9+
import android.view.View;
10+
import android.view.ViewGroup;
11+
import android.widget.BaseAdapter;
12+
import android.widget.ListView;
13+
14+
/**
15+
* Created by Gavin on 2016/11/20.
16+
*/
17+
18+
public class DemoListActivity extends AppCompatActivity {
19+
20+
@Override
21+
protected void onCreate(@Nullable Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_demo_list);
24+
25+
ListView listView = (ListView) findViewById(R.id.list);
26+
listView.setAdapter(new Adapter(getApplicationContext()));
27+
}
28+
29+
@NonNull
30+
@Override
31+
public LayoutInflater getLayoutInflater() {
32+
return super.getLayoutInflater();
33+
}
34+
35+
private static class Adapter extends BaseAdapter {
36+
37+
private Context ctx;
38+
39+
private Adapter(Context ctx) {
40+
this.ctx = ctx;
41+
}
42+
43+
@Override
44+
public int getCount() {
45+
return 20;
46+
}
47+
48+
@Override
49+
public Object getItem(int position) {
50+
return null;
51+
}
52+
53+
@Override
54+
public long getItemId(int position) {
55+
return position;
56+
}
57+
58+
@Override
59+
public View getView(int position, View convertView, ViewGroup parent) {
60+
if (convertView == null) {
61+
convertView = LayoutInflater.from(ctx).inflate(R.layout.item_demo_list, parent, false);
62+
}
63+
return convertView;
64+
}
65+
}
66+
67+
}
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package cn.gavinliu.android_scalelayout;
22

3-
import android.content.Context;
3+
import android.content.Intent;
44
import android.os.Bundle;
55
import android.support.v7.app.AppCompatActivity;
6-
import android.util.AttributeSet;
76
import android.view.View;
87

98
public class MainActivity extends AppCompatActivity {
@@ -14,16 +13,14 @@ protected void onCreate(Bundle savedInstanceState) {
1413
setContentView(R.layout.activity_main);
1514
}
1615

17-
@Override
18-
public View onCreateView(String name, Context context, AttributeSet attrs) {
19-
// switch (name) {
20-
// case "FrameLayout":
21-
// return new ScaleFrameLayout(context, attrs);
22-
// case "LinearLayout":
23-
// return new ScaleLinearLayout(context, attrs);
24-
// case "RelativeLayout":
25-
// return new ScaleRelativeLayout(context, attrs);
26-
// }
27-
return super.onCreateView(name, context, attrs);
16+
public void demo(View view) {
17+
Intent intent = new Intent(this, DemoActivity.class);
18+
startActivity(intent);
19+
}
20+
21+
public void demoList(View view) {
22+
Intent intent = new Intent(this, DemoListActivity.class);
23+
startActivity(intent);
2824
}
25+
2926
}

Sample/src/main/java/cn/gavinliu/android_scalelayout/MainApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class MainApplication extends Application {
1313
@Override
1414
public void onCreate() {
1515
super.onCreate();
16-
ScaleConfig.create(this, 1080, 1920, 3);
16+
ScaleConfig.create(this, 1080, 1920, 3, ScaleConfig.DIMENS_UNIT_DP);
17+
ScaleConfig.getInstance().setDebug(true);
1718
}
1819
}

0 commit comments

Comments
 (0)