github 地址:github.com/bugyun/Scre… 歡迎 star 和 提問。java
插件版本:android
在項目的根 build.gradle 中添加,若是jcenter 倉庫找不到項目,那麼能夠添加個人倉庫git
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/bugyun/maven" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-alpha13'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// 在此處添加
classpath 'vip.ruoyun.plugin:screen-plugin:1.0.0'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/bugyun/maven" }
}
}
複製代碼
在要使用插件的的子項目的 build.gradle 中添加github
apply plugin: 'vip.ruoyun.screen'
screen {
smallestWidths 320, 360, 384, 392, 400, 410, 411, 432, 480 //生成的目標屏幕寬度的適配文件
designSmallestWidth 375 //蘋果設計稿750 × 1334 屏幕寬度爲 375
decimalFormat "#.#" //設置保留的小數 ( #.## 保留2位) ( #.# 保留1位)
log false //是否打印日誌
auto false //是否每次 build 項目的時候自動生成 values-sw[]dp 文件
}
複製代碼
若是 auto 設置爲 true ,則每次 build 項目的時候自動生成 values-sw[]dp 文件 若是 auto 設置爲 false,則能夠經過命令行,來生成文件.bash
./gradlew dimensCovert
複製代碼
也能夠在 gradle命令的 窗口中 點擊 dimensCovert 的 task.app
只會生成 dp 後綴的屬性值,根據 values 目錄下的 dimens.xml,生成具體的文件。maven
values 目錄下的 dimens.xmlide
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="x20">20dp</dimen>
<dimen name="x30">20sp</dimen>
</resources>
複製代碼
生成的目標文件,values-sw320dp 目錄下的 dimens.xml 文件post
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<dimen name="x20">17.1dp</dimen>
</resources>
複製代碼
在項目的根 build.gradle 中添加 jcenter ,若是jcenter 倉庫找不到項目,那麼能夠添加個人倉庫
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/bugyun/maven" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-alpha13'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://dl.bintray.com/bugyun/maven" }
}
}
複製代碼
而後在子項目中的 build.gradle 文件中添加
dependencies {
implementation 'vip.ruoyun.helper:screen-helper:1.0.0'
}
複製代碼
使用,在每一個Activity 重寫getResources()方法。
public class MainActivity extends AppCompatActivity {
@Override
public Resources getResources() {
return ScreenHelper.applyAdapt(super.getResources(), 450f, ScreenHelper.ScreenMode.WIDTH_DP);
}
}
複製代碼
若是是懸浮窗適配,由於 inflate 用到的 context 是 application 級別的,因此須要在自定義的 Application 中重寫 getResource。
public class App extends Application {
@Override
public Resources getResources() {
return ScreenHelper.applyAdapt(super.getResources(), 450f, ScreenHelper.ScreenMode.WIDTH_DP);
}
}
複製代碼
關閉適配
ScreenHelper.closeAdapt(super.getResources());
複製代碼
轉換單位的方法
/**
* Value of pt/dp to value of px.
*/
public static int value2px(Context context, float value, int screenMode) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
switch (screenMode) {
case ScreenMode.WIDTH_DP:
default:
return (int) (value * metrics.density);
case ScreenMode.HEIGHT_PT:
case ScreenMode.WIDTH_PT:
return (int) (value * metrics.xdpi / 72f + 0.5);
}
}
/**
* Value of px to value of pt/dp.
*/
public static int px2Value(Context context, float pxValue, int screenMode) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
switch (screenMode) {
case ScreenMode.WIDTH_DP:
default:
return (int) (pxValue / metrics.density);
case ScreenMode.HEIGHT_PT:
case ScreenMode.WIDTH_PT:
return (int) (pxValue * 72 / metrics.xdpi + 0.5);
}
}
複製代碼
感謝大神總結的方案和思路,我是在這個基礎上總結,讓你們更方便的使用這些技術。