Android屏幕适配的两种方式

栏目: Java · 发布时间: 5年前

内容简介:在项目的根 build.gradle 中添加,如果jcenter 仓库找不到项目,那么可以添加我的仓库在要使用插件的的子项目的 build.gradle 中添加如果 auto 设置为 true ,则每次 build 项目的时候自动生成 values-sw[]dp 文件 如果 auto 设置为 false,则可以通过命令行,来生成文件.

在项目的根 build.gradle 中添加,如果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"
        // 在此处添加
        classpath 'vip.ruoyun.plugin:screen-plugin:1.0.0'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://dl.bintray.com/bugyun/maven" }
    }
}
复制代码

在要使用插件的的子项目的 build.gradle 中添加

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,则可以通过命令行,来生成文件.

./gradlew dimensCovert
复制代码

也可以在 gradle命令的 窗口中 点击 dimensCovert 的 task.

Android屏幕适配的两种方式

自动生成的sw 文件

Android屏幕适配的两种方式

生成规则:

只会生成 dp 后缀的属性值,根据 values 目录下的 dimens.xml,生成具体的文件。

values 目录下的 dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="x20">20dp</dimen>
    <dimen name="x30">20sp</dimen>
</resources>
复制代码

生成的目标文件,values-sw320dp 目录下的 dimens.xml 文件

<?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);
    }
}
复制代码

类型

  • ScreenHelper.ScreenMode.WIDTH_DP 以 dp 来适配,在 xml 中使用 dp 单位
  • ScreenHelper.ScreenMode.WIDTH_PT 以 pt 来适配,在 xml 中使用 pt 单位
  • ScreenHelper.ScreenMode.HEIGHT_PT 以 pt 来适配,在 xml 中使用 pt 单位

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

About Face 3

About Face 3

Alan Cooper、Robert Reimann、David Cronin / John Wiley & Sons / 2007-5-15 / GBP 28.99

* The return of the authoritative bestseller includes all new content relevant to the popularization of how About Face maintains its relevance to new Web technologies such as AJAX and mobile platforms......一起来看看 《About Face 3》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码