Giter Site home page Giter Site logo

liheng2016 / screenhelper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bugyun/screenhelper

0.0 1.0 0.0 286 KB

android 屏幕适配的终极方案: SmallestWidth适配修改和 DisplayMetrics.densityDpi属性, 完美兼容 AndroidX 和 Android 库 ^_^ ,欢迎使用~~

Home Page: https://juejin.im/post/5cd2e87ee51d4514df420740

License: Apache License 2.0

Kotlin 8.48% Java 51.37% Groovy 40.15%

screenhelper's Introduction

ScreenHelper

android 屏幕适配的两种终结方式

  • Smallest Width 适配
  • 修改 DisplayMetrics.densityDpi 属性

完美兼容 AndroidX 和 Android 库 ^_^ ,欢迎使用~~


第一种适配方式 - Smallest Width 方式

插件版本: Download

使用方法

在项目的根 build.gradle 中添加,如果jcenter 仓库找不到项目,那么可以添加我的仓库

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()
        //maven { url "https://dl.bintray.com/bugyun/maven" } //我的仓库,如果 jcenter 能找到,就不要添加
    }
    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" } //我的仓库,如果 jcenter 能找到,就不要添加
    }
}

在要使用插件的的子项目的 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.

自动生成的sw 文件

生成规则:只会生成 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>

第二种适配方式 - 通过代码来实现适配

插件版本: Download

在项目的根 build.gradle 中添加 jcenter ,如果jcenter 仓库找不到项目,那么可以添加我的仓库

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        google()
        jcenter()
        //maven { url "https://dl.bintray.com/bugyun/maven" }//我的仓库,如果 jcenter 能找到,就不要添加
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        //maven { url "https://dl.bintray.com/bugyun/maven" }//我的仓库,如果 jcenter 能找到,就不要添加
    }
}

然后在子项目中的 build.gradle 文件中添加

dependencies {
    implementation 'vip.ruoyun.helper:screen-helper:1.0.1'
}

使用,在每个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.ScreenMode.WIDTH_DP 以 dp 来适配,在 xml 中使用 dp 单位
  • ScreenHelper.ScreenMode.WIDTH_PT 以 pt 来适配,在 xml 中使用 pt 单位
  • ScreenHelper.ScreenMode.HEIGHT_PT 以 pt 来适配,在 xml 中使用 pt 单位

版本变化

  • 1.0.1 :优化 Resources.getSystem() 变量获取,由于此方法是 synchronized ,如果频繁调用会影响性能
  • 1.0.0 :正式发版

screenhelper's People

Contributors

bugyun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.