项目作者: chRyNaN

项目描述 :
Kotlin Inline Classes for Screen Dimension Units
高级语言: Kotlin
项目地址: git://github.com/chRyNaN/pixel.git
创建时间: 2018-11-15T21:42:16Z
项目社区:https://github.com/chRyNaN/pixel

开源协议:Apache License 2.0

下载


pixel

Kotlin Classes for Screen Dimension Units

This Kotlin Multi-platform library provides type safety over screen units, such as pixels. Available ScreenDimensionUnits interface implementations include:

  • Pixels (PX)
  • DependencyIndependentPixels (DIP/DP)
  • ScaledPixels (SP)
  • PointPixels (PT)

This provides type safety of knowing what you are working with (ex: fun drawLine(start: Pixels, end: Pixels)).

Building the library

The library is provided through Bintray. Refer to the releases for the latest version.

Repository

  1. repositories {
  2. maven {
  3. url = uri("https://dl.bintray.com/chrynan/chrynan")
  4. }
  5. }

Dependencies

Base Kotlin Common Library:

  1. implementation 'com.chrynan.pixel:pixel-core:VERSION'

Base Kotlin JVM Library:

  1. implementation 'com.chrynan.pixel:pixel-core-jvm:VERSION'

Base Kotlin JS Library:

  1. implementation 'com.chrynan.pixel:pixel-core-js:VERSION'

Android Library:

  1. implementation 'com.chrynan.pixel:pixel-android:VERSION'

Android Jetpack Compose Library:

  1. implementation 'com.chrynan.pixel:pixel-android-compose:VERSION'

Using the library

  • Setup the ScreenDimensionUnitConverter on the Pixel object.

Android:

  1. class MyApplication : Application {
  2. override fun onCreate() {
  3. super.onCreate()
  4. Pixel.converter = AndroidScreenDimensionUnitConverter(context = this)
  5. }
  6. }

Kotlin JS:

  1. Pixel.converter = JsScreenDimensionUnitConverter()
  • Convert any Kotlin Number to a particular ScreenDimensionUnit:
    ```kotlin
    val pixels = 100.0.px
    val scaledPixels = 25.sp
    val densityIndependentPixels = 12f.dp
    val pointPixels = 5L.pt

val pixelSum = 10.px + 20.px

if (pixels > 150.px) { }

  1. ## Converting between Android Jetpack Compose Values
  2. The Android Jetpack Compose framework comes with representations of `Dp` and `Px` values. This `pixel-android-compose` library provides ways to convert to and from the Android Jetpack Compose values.
  3. ```kotlin
  4. val composePx = pxValue.inComposePx
  5. val composeDp = dpValue.inComposeDp
  6. val px = composePx.inPx
  7. val dp = composeDp.inDp