Repository: gavinliu/Android-ScaleLayout Branch: master Commit: a62b7c269ac8 Files: 35 Total size: 53.5 KB Directory structure: gitextract_w1ffappy/ ├── .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 │ ├── values/ │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── values-w820dp/ │ └── dimens.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 └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ #Android generated bin gen gen* #Eclipse .project .classpath .settings #IntelliJ IDEA .idea *.iml *.ipr *.iws out #Maven target release.properties pom.xml.* #Ant build.xml local.properties proguard.cfg #Gradle .gradle build #OSX .DS_Store #Personal Files signing. # Built application files *.apk *.ap_ # Files for the Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log gradle.properties gradle/ gradlew gradlew.bat ================================================ FILE: README.md ================================================ # Android-ScaleLayout A Simple & Convenience MultiScreen-Support-Library for Android ## The essence is percent scaling. ### different from ``android-percent-support-lib`` 1. **More reliable** ``android-percent-support-lib`` The percentage of the parent and child views. ``Android-ScaleLayout`` The percentage of the design and devices screens. 2. **More convenience** ``android-percent-support-lib`` need to calculate percent. ``Android-ScaleLayout`` directly write the design size on ``layout.xml``. ## How to look? ![screenhot](/screenhot.png) ## Principle ```java float realPixel = percent * designPixel ``` ### Pix Mode ```java float realPixel = percent * designPixel float percent = mScreenWidth / designScreenWidth float designPixel = res.getDimensionPixelSize() ``` ```java float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth ``` ### DP Mode ```java float realPixel = percent * designPixel float percent = mScreenWidth / designScreenWidth float designPixel = designDP * designDensity // dp to pixel float designDP = res.getDimensionPixelSize() / mDensity ``` ```java float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity) ``` ## Usage ### 0. dependencies ``` dependencies { compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' } ``` ### 1. Initialize ```java public class MyApplication extends Application { @Override public void onCreate() { ScaleConfig.create(this, 1080, // Design Width 1920, // Design Height 3, // Design Density 3, // Design FontScale ScaleConfig.DIMENS_UNIT_DP); } } ``` > ``TypedValue.COMPLEX_UNIT_SP`` is Android FontSize unit, the ``fontscale``: > float fontScale = ctx.getResources().getDisplayMetrics().scaledDensity; ### 2. Scale***Layout Only need to replace ``FrameLayout`` ``LinearLayout`` ``RelativeLayout`` to ``ScaleFrameLayout`` ``ScaleLinearLayout`` ``ScaleRelativeLayout``. ### 3. Scale by width or height Width is default, you can also changed using attr. ```xml ``` ```xml app:layout_scale_by="width" ``` ### Support Attrs ```xml ``` ## License MIT ================================================ FILE: README_zh.md ================================================ # Android-ScaleLayout 一个简单的,方便的多屏适配的Android库 ## 本质就是百分比缩放 ### 和 android-percent-support-lib 的不同 1. **更科学** ``android-percent-support-lib`` 是父控件和子控件的百分比关系 ``Android-ScaleLayout`` 是设计界面和设备界面的百分比关系 2. **更方便** ``android-percent-support-lib`` 需要把设计尺寸算成百分比 ``Android-ScaleLayout`` 直接把设计尺寸填入``layou.xml``即可 ## How to look? ![screenhot](/screenhot.png) ## 原理 ```java float realPixel = percent * designPixel ``` ### Pix Mode ```java float realPixel = percent * designPixel float percent = mScreenWidth / designScreenWidth float designPixel = res.getDimensionPixelSize() ``` ```java float realPixel = mScreenWidth * res.getDimensionPixelSize() / designScreenWidth ``` ### DP Mode ```java float realPixel = percent * designPixel float percent = mScreenWidth / designScreenWidth float designPixel = designDP * designDensity // dp to pixel float designDP = res.getDimensionPixelSize() / mDensity ``` ```java float realPixel = (mScreenWidth * designDensity * getPixelSize()) / (designScreenWidth * mDensity) ``` ## 使用 ### 0. 依赖 ``` dependencies { compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' } ``` ### 1. 初始化 ```java public class MyApplication extends Application { @Override public void onCreate() { ScaleConfig.create(this, 1080, // Design Width 1920, // Design Height 3, // Design Density 3, // Design FontScale ScaleConfig.DIMENS_UNIT_DP); } } ``` > Android 文字大小的单位是 ``sp``,字体的缩放方式需要 FontScale: > float fontScale = ctx.getResources().getDisplayMetrics().scaledDensity; ### 2. Scale***Layout 只需要把 ``FrameLayout`` ``LinearLayout`` ``RelativeLayout`` 替换成 ``ScaleFrameLayout`` ``ScaleLinearLayout`` ``ScaleRelativeLayout``. ### 3. Scale by width or height 默认是 width,当然你可以用属性修改。 ```xml ``` ```xml app:layout_scale_by="width" ``` ### 支持的属性 ```xml ``` ## License MIT ================================================ FILE: Sample/.gitignore ================================================ /build ================================================ FILE: Sample/build.gradle ================================================ apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicationId "cn.gavinliu.android_scalelayout" minSdkVersion 9 targetSdkVersion 24 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles 'proguard-rules.pro' } } } dependencies { // compile project(':ScaleLayout') compile 'cn.gavinliu.android.lib:ScaleLayout:1.0.4' compile 'com.android.support:appcompat-v7:24.2.0' } ================================================ FILE: Sample/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /home/gavin/Develop/android-sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} ================================================ FILE: Sample/src/androidTest/java/cn/gavinliu/android_scalelayout/ApplicationTest.java ================================================ package cn.gavinliu.android_scalelayout; import android.app.Application; import android.test.ApplicationTestCase; /** * Testing Fundamentals */ public class ApplicationTest extends ApplicationTestCase { public ApplicationTest() { super(Application.class); } } ================================================ FILE: Sample/src/main/AndroidManifest.xml ================================================ ================================================ FILE: Sample/src/main/java/cn/gavinliu/android_scalelayout/DemoActivity.java ================================================ package cn.gavinliu.android_scalelayout; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * Created by Gavin on 2016/11/20. */ public class DemoActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); } } ================================================ FILE: Sample/src/main/java/cn/gavinliu/android_scalelayout/DemoListActivity.java ================================================ package cn.gavinliu.android_scalelayout; import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; /** * Created by Gavin on 2016/11/20. */ public class DemoListActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo_list); ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(new Adapter(getApplicationContext())); } @NonNull @Override public LayoutInflater getLayoutInflater() { return super.getLayoutInflater(); } private static class Adapter extends BaseAdapter { private Context ctx; private Adapter(Context ctx) { this.ctx = ctx; } @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(ctx).inflate(R.layout.item_demo_list, parent, false); } return convertView; } } } ================================================ FILE: Sample/src/main/java/cn/gavinliu/android_scalelayout/MainActivity.java ================================================ package cn.gavinliu.android_scalelayout; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void demo(View view) { Intent intent = new Intent(this, DemoActivity.class); startActivity(intent); } public void demoList(View view) { Intent intent = new Intent(this, DemoListActivity.class); startActivity(intent); } } ================================================ FILE: Sample/src/main/java/cn/gavinliu/android_scalelayout/MainApplication.java ================================================ package cn.gavinliu.android_scalelayout; import android.app.Application; import cn.gavinliu.android.lib.scale.config.ScaleConfig; /** * Created by Gavin on 16-11-10. */ public class MainApplication extends Application { @Override public void onCreate() { super.onCreate(); ScaleConfig.create(this, 1080, // Design Width 1920, // Design Height 3, // Design Density 3, // Design FontScale ScaleConfig.DIMENS_UNIT_DP); ScaleConfig.getInstance().setDebug(true); } } ================================================ FILE: Sample/src/main/res/layout/activity_demo.xml ================================================ ================================================ FILE: Sample/src/main/res/layout/activity_demo_list.xml ================================================ ================================================ FILE: Sample/src/main/res/layout/activity_main.xml ================================================