Giter Site home page Giter Site logo

dimension-extensions's Introduction

dp px 转换工具类 by Kotlin

Step 1. Add the dependency

dependencies {
       api 'com.pdog.util:dimension-extensions:0.9.0'
}

Step 2. Use in Kotlin's code(or other file)

val height = 48f.dp

说明

android开发中有的时候处理控件大小我们会需要用到dp值来进行处理,针对这种情况,我们一般会写一个类似dp2px(float dp)static方法。

然后每次调用的时候这样来使用

float height = dp2px(48f)
//use height

当阅读代码时,我们会脑补一个48fdp2px() 函数调用的“回头”操作, 阅读代码的顺序大概是这样的

float height = dp2px(48f)
float height  //定义一个height
dp2px(48f)  // height 值是一个函数结果,函数参数是48f
dp2px(48f)  // 再次,48f是从dp转换成px

简直就是石器时代的代码啊!

现在我们到了工业时代的kotlin 里用扩展字段。我可以将代码写成这样

val height = 48f.dp

定义一个height 值为 48f 对应的dp 值 可以读出来的代码!真香!


扩展字段源码

  1. 无关Context 不需要传入 context调用 ,也不需要在 application初始化,复制即用!
  2. 测试通过displayMetricsActivity 被反射修改后,会使用反射后的值来进行转换
package com.pdog.dimension

import android.content.res.Resources

/**
 * 正常编码中一般只会用到 [dp]/[sp] ;
 * 其中[dp]/[sp] 会根据系统分辨率将输入的dp/sp值转换为对应的px
 */
val Float.dp: Float                 // [xxhdpi](360 -> 1080)
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics)

val Int.dp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics).toInt()


val Float.sp: Float                 // [xxhdpi](360 -> 1080)
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics)


val Int.sp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP, this.toFloat(), Resources.getSystem().displayMetrics).toInt()

dimension-extensions's People

Contributors

pdog18 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.