All Projects → bugyun → ScreenHelper

bugyun / ScreenHelper

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

Programming Languages

java
68154 projects - #9 most used programming language
groovy
2714 projects
kotlin
9241 projects

Projects that are alternatives of or similar to ScreenHelper

Questions
A modular iOS quiz app
Stars: ✭ 108 (+200%)
Mutual labels:  app
11tyby
Simple 11ty setup using TypeScript, SASS, Preact with partial hydration, and other useful things. Aims to provide the DX of Gatsby, but using 11ty!
Stars: ✭ 38 (+5.56%)
Mutual labels:  app
CSwala-android
An app that is a one-stop destination for all the CS enthusiasts, providing resources like Information scrapping techniques, best YT channels, courses available free-of-cost, etc. & knowledge about every domain and field that exists on the Internet related to Computer Science along with News, Jobs, and Internships opportunities in these domains …
Stars: ✭ 44 (+22.22%)
Mutual labels:  app
currency-converter
💰 Easily convert between 32 currencies
Stars: ✭ 16 (-55.56%)
Mutual labels:  app
lacmus-app
lacmus-app
Stars: ✭ 34 (-5.56%)
Mutual labels:  app
smart-home-neomorphism-app
Smart Home app ui design in a neomorphism style.
Stars: ✭ 82 (+127.78%)
Mutual labels:  app
SimpleScreens
Simple Screens is a library of screens and screen components (forms, sections, transitions) to be included, extended, or generally reused in applications based on Moqui Framework and Mantle Business Artifacts.
Stars: ✭ 20 (-44.44%)
Mutual labels:  screen
ProxerAndroid
The official Android App of Proxer.Me
Stars: ✭ 105 (+191.67%)
Mutual labels:  app
bbb app
Our attempt at a mobile app client for BigBlueButton services.
Stars: ✭ 83 (+130.56%)
Mutual labels:  app
rewind-docs
垃圾应用主页
Stars: ✭ 73 (+102.78%)
Mutual labels:  app
nimblenote
Simple keyboard-driven note taking application for macOS, Linux and Windows.
Stars: ✭ 31 (-13.89%)
Mutual labels:  app
superhero
Convert a Redux app to React Context API App
Stars: ✭ 23 (-36.11%)
Mutual labels:  app
IndianComedyapp
Indian Comedy shows.This app contains all Indian famous comedy videos.you can download and watch all your favorite comedy videos .the database used here is mysqli and for connection I used php.this app also contains a funny comedy timeline like fb. Used Json and Volley library for Loading datin in a very fast way also provides Cache during offil…
Stars: ✭ 13 (-63.89%)
Mutual labels:  app
EasyAES
AES encrypt/decrypt, Android, iOS, php compatible(兼容php, Android, iOS平台)
Stars: ✭ 79 (+119.44%)
Mutual labels:  app
taro-playground
The Taro Playground App is a cross-platform application developed using Taro, to help developers develop and debug Taro applications.
Stars: ✭ 33 (-8.33%)
Mutual labels:  app
MMM-MagicMover
MagicMirror² module to avoid screen burn-in
Stars: ✭ 21 (-41.67%)
Mutual labels:  screen
hms-ecommerce-demo
Build a shopping app by HMS Core kits which shows how to use the HMS Core solution in E-Commerce industry.
Stars: ✭ 70 (+94.44%)
Mutual labels:  app
jyutping
Cantonese Jyutping Keyboard for iOS. 粵語粵拼輸入法鍵盤
Stars: ✭ 23 (-36.11%)
Mutual labels:  app
IT-Security-Quiz-App
IT Security Quiz App for Android. Project for Android Basics: User Input course by Udacity
Stars: ✭ 15 (-58.33%)
Mutual labels:  app
screenAdaptation
Android screen UI adaptation
Stars: ✭ 24 (-33.33%)
Mutual labels:  screen

alt text

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

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

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

原理讲解,必看

屏幕原理

适配原理


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

插件版本: Download

使用方法

在项目的根 build.gradle 中添加

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0-alpha13'
        // 在此处添加
        classpath 'vip.ruoyun.plugin:screen-plugin:1.0.1'
    }
}
allprojects {
    repositories {
        google()
        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>

第二种适配方式 - DisplayMetrics.densityDpi 属性修改

插件版本: Download

在项目的根 build.gradle 中添加 jcenter 仓库

buildscript {
    repositories {
        google()
        jcenter()
    }
    ...
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

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

使用,在每个Activity 重写getResources()方法。

public class MainActivity extends AppCompatActivity {
    @Override
    public Resources getResources() {
        return ScreenHelper.applyAdapt(super.getResources(), 450f, ScreenHelper.WIDTH_DP);
    }
}

如果是悬浮窗适配,因为 inflate 用到的 context 是 application 级别的,所以需要在自定义的 Application 中重写 getResource。

public class App extends Application {
    @Override
    public Resources getResources() {
        return ScreenHelper.applyAdapt(super.getResources(), 450f, ScreenHelper.WIDTH_DP);
    }
}

类型

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

版本变化

  • 1.0.2 :优化传递 ScreenMode 的参数传递,去除不必要的 log
  • 1.0.1 :优化 Resources.getSystem() 变量获取,由于此方法是 synchronized ,如果频繁调用会影响性能
  • 1.0.0 :正式发版
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].