├── .gitignore ├── README.md ├── README_zh.md ├── Sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── gavinliu │ │ └── android_scalelayout │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── gavinliu │ │ └── android_scalelayout │ │ ├── DemoActivity.java │ │ ├── DemoListActivity.java │ │ ├── MainActivity.java │ │ └── MainApplication.java │ └── res │ ├── layout │ ├── activity_demo.xml │ ├── activity_demo_list.xml │ ├── activity_main.xml │ └── item_demo_list.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── ScaleLayout ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── gavinliu │ │ └── android │ │ └── lib │ │ └── scale │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── cn │ │ └── gavinliu │ │ └── android │ │ └── lib │ │ └── scale │ │ ├── ScaleFrameLayout.java │ │ ├── ScaleLinearLayout.java │ │ ├── ScaleRelativeLayout.java │ │ ├── config │ │ └── ScaleConfig.java │ │ └── helper │ │ └── ScaleLayoutHelper.java │ └── res │ └── values │ ├── attrs.xml │ └── strings.xml ├── build.gradle ├── screenhot.png └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | #Android generated 2 | bin 3 | gen 4 | gen* 5 | 6 | #Eclipse 7 | .project 8 | .classpath 9 | .settings 10 | 11 | #IntelliJ IDEA 12 | .idea 13 | *.iml 14 | *.ipr 15 | *.iws 16 | out 17 | 18 | #Maven 19 | target 20 | release.properties 21 | pom.xml.* 22 | 23 | #Ant 24 | build.xml 25 | local.properties 26 | proguard.cfg 27 | 28 | #Gradle 29 | .gradle 30 | build 31 | 32 | #OSX 33 | .DS_Store 34 | 35 | #Personal Files 36 | signing. 37 | 38 | 39 | # Built application files 40 | *.apk 41 | *.ap_ 42 | 43 | # Files for the Dalvik VM 44 | *.dex 45 | 46 | # Java class files 47 | *.class 48 | 49 | # Generated files 50 | bin/ 51 | gen/ 52 | 53 | # Gradle files 54 | .gradle/ 55 | build/ 56 | 57 | # Local configuration file (sdk path, etc) 58 | local.properties 59 | 60 | # Proguard folder generated by Eclipse 61 | proguard/ 62 | 63 | # Log Files 64 | *.log 65 | 66 | gradle.properties 67 | gradle/ 68 | gradlew 69 | gradlew.bat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-ScaleLayout 2 | 3 | A Simple & Convenience MultiScreen-Support-Library for Android 4 | 5 | ## The essence is percent scaling. 6 | 7 | ### different from ``android-percent-support-lib`` 8 | 9 | 1. **More reliable** 10 | ``android-percent-support-lib`` The percentage of the parent and child views. 11 | ``Android-ScaleLayout`` The percentage of the design and devices screens. 12 | 13 | 2. **More convenience** 14 | ``android-percent-support-lib`` need to calculate percent. 15 | ``Android-ScaleLayout`` directly write the design size on ``layout.xml``. 16 | 17 | 18 | ## How to look? 19 | 20 | ![screenhot](/screenhot.png) 21 | 22 | 23 | ## Principle 24 | 25 | ```java 26 | float realPixel = percent * designPixel 27 | ``` 28 | 29 | ### Pix Mode 30 | 31 | ```java 32 | float realPixel = percent * designPixel 33 | 34 | float percent = mScreenWidth / designScreenWidth 35 | 36 | float designPixel = res.getDimensionPixelSize() 37 | ``` 38 | ```java 39 | float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth 40 | ``` 41 | 42 | ### DP Mode 43 | ```java 44 | float realPixel = percent * designPixel 45 | 46 | float percent = mScreenWidth / designScreenWidth 47 | float designPixel = designDP * designDensity // dp to pixel 48 | 49 | float designDP = res.getDimensionPixelSize() / mDensity 50 | ``` 51 | ```java 52 | float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity) 53 | ``` 54 | 55 | ## Usage 56 | 57 | ### 0. dependencies 58 | 59 | ``` 60 | dependencies { 61 | compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' 62 | } 63 | ``` 64 | 65 | ### 1. Initialize 66 | 67 | ```java 68 | public class MyApplication extends Application { 69 | 70 | @Override 71 | public void onCreate() { 72 | ScaleConfig.create(this, 73 | 1080, // Design Width 74 | 1920, // Design Height 75 | 3, // Design Density 76 | 3, // Design FontScale 77 | ScaleConfig.DIMENS_UNIT_DP); 78 | } 79 | } 80 | ``` 81 | 82 | > ``TypedValue.COMPLEX_UNIT_SP`` is Android FontSize unit, the ``fontscale``: 83 | > float fontScale = ctx.getResources().getDisplayMetrics().scaledDensity; 84 | 85 | ### 2. Scale***Layout 86 | 87 | Only need to replace ``FrameLayout`` ``LinearLayout`` ``RelativeLayout`` to ``ScaleFrameLayout`` ``ScaleLinearLayout`` ``ScaleRelativeLayout``. 88 | 89 | ### 3. Scale by width or height 90 | 91 | Width is default, you can also changed using attr. 92 | 93 | ```xml 94 | 95 | 96 | 97 | 98 | ``` 99 | ```xml 100 | app:layout_scale_by="width" 101 | ``` 102 | 103 | ### Support Attrs 104 | 105 | ```xml 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | ``` 128 | 129 | ## License 130 | 131 | MIT 132 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 | # Android-ScaleLayout 2 | 3 | 一个简单的,方便的多屏适配的Android库 4 | 5 | ## 本质就是百分比缩放 6 | 7 | ### 和 android-percent-support-lib 的不同 8 | 9 | 1. **更科学** 10 | ``android-percent-support-lib`` 是父控件和子控件的百分比关系 11 | ``Android-ScaleLayout`` 是设计界面和设备界面的百分比关系 12 | 13 | 2. **更方便** 14 | ``android-percent-support-lib`` 需要把设计尺寸算成百分比 15 | ``Android-ScaleLayout`` 直接把设计尺寸填入``layou.xml``即可 16 | 17 | 18 | ## How to look? 19 | 20 | ![screenhot](/screenhot.png) 21 | 22 | 23 | ## 原理 24 | 25 | ```java 26 | float realPixel = percent * designPixel 27 | ``` 28 | 29 | ### Pix Mode 30 | 31 | ```java 32 | float realPixel = percent * designPixel 33 | 34 | float percent = mScreenWidth / designScreenWidth 35 | 36 | float designPixel = res.getDimensionPixelSize() 37 | ``` 38 | ```java 39 | float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth 40 | ``` 41 | 42 | ### DP Mode 43 | ```java 44 | float realPixel = percent * designPixel 45 | 46 | float percent = mScreenWidth / designScreenWidth 47 | float designPixel = designDP * designDensity // dp to pixel 48 | 49 | float designDP = res.getDimensionPixelSize() / mDensity 50 | ``` 51 | ```java 52 | float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity) 53 | ``` 54 | 55 | ## 使用 56 | 57 | ### 0. 依赖 58 | 59 | ``` 60 | dependencies { 61 | compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' 62 | } 63 | ``` 64 | 65 | ### 1. 初始化 66 | 67 | ```java 68 | public class MyApplication extends Application { 69 | 70 | @Override 71 | public void onCreate() { 72 | ScaleConfig.create(this, 73 | 1080, // Design Width 74 | 1920, // Design Height 75 | 3, // Design Density 76 | 3, // Design FontScale 77 | ScaleConfig.DIMENS_UNIT_DP); 78 | } 79 | } 80 | ``` 81 | 82 | > Android 文字大小的单位是 ``sp``,字体的缩放方式需要 FontScale: 83 | > float fontScale = ctx.getResources().getDisplayMetrics().scaledDensity; 84 | 85 | ### 2. Scale***Layout 86 | 87 | 只需要把 ``FrameLayout`` ``LinearLayout`` ``RelativeLayout`` 替换成 ``ScaleFrameLayout`` ``ScaleLinearLayout`` ``ScaleRelativeLayout``. 88 | 89 | ### 3. Scale by width or height 90 | 91 | 默认是 width,当然你可以用属性修改。 92 | 93 | ```xml 94 | 95 | 96 | 97 | 98 | ``` 99 | ```xml 100 | app:layout_scale_by="width" 101 | ``` 102 | 103 | ### 支持的属性 104 | 105 | ```xml 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | ``` 128 | 129 | ## License 130 | 131 | MIT 132 | -------------------------------------------------------------------------------- /Sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.gavinliu.android_scalelayout" 9 | minSdkVersion 9 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | // compile project(':ScaleLayout') 24 | compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' 25 | 26 | compile 'com.android.support:appcompat-v7:24.2.0' 27 | } 28 | -------------------------------------------------------------------------------- /Sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/gavin/Develop/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /Sample/src/androidTest/java/cn/gavinliu/android_scalelayout/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.android_scalelayout; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /Sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Sample/src/main/java/cn/gavinliu/android_scalelayout/DemoActivity.java: -------------------------------------------------------------------------------- 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 | } 19 | -------------------------------------------------------------------------------- /Sample/src/main/java/cn/gavinliu/android_scalelayout/DemoListActivity.java: -------------------------------------------------------------------------------- 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 | } 68 | -------------------------------------------------------------------------------- /Sample/src/main/java/cn/gavinliu/android_scalelayout/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.gavinliu.android_scalelayout; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | } 15 | 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); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Sample/src/main/java/cn/gavinliu/android_scalelayout/MainApplication.java: -------------------------------------------------------------------------------- 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, 17 | 1080, // Design Width 18 | 1920, // Design Height 19 | 3, // Design Density 20 | 3, // Design FontScale 21 | ScaleConfig.DIMENS_UNIT_DP); 22 | ScaleConfig.getInstance().setDebug(true); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sample/src/main/res/layout/activity_demo.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 14 | 15 | 26 | 27 | 37 | 38 | 39 | 40 | 47 | 48 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /Sample/src/main/res/layout/activity_demo_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | -------------------------------------------------------------------------------- /Sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 |