Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
yesitskev committed Feb 1, 2017
0 parents commit 12c47c9
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.classpath
.project
.settings
eclipsebin

bin
gen
build
out
lib

.idea
*.iml
classes

obj

.DS_Store

# Gradle
.gradle
jniLibs
build
local.properties
reports
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Change Log
==========

Version 0.1.0 *(2017-02-01)*
----------------------------

Initial version.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Android Units
==========

A utility class for converting between different Android display units



Example Usage
-------------

How can I get the pixels for the display pixel (dp) measurement I have?
```java
float topPaddingOrSomething = AndroidUnits.DENSITY_PIXELS.toPixels(8);
```


Download
--------

Add via Gradle:
```groovy
compile 'com.github.kevelbreh:androidunits:0.1.0'
```
or Maven:
```xml
<dependency>
<groupId>com.github.kevelbre</groupId>
<artifactId>androidunits</artifactId>
<version>0.1.0</version>
</dependency>
```


License
-------

Copyright 2017 Kevin Woodland

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
22 changes: 22 additions & 0 deletions androidunits/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group='com.github.kevelbreh'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 9
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.kevelbreh.androidunits;

import android.content.res.Resources;
import android.support.test.runner.AndroidJUnit4;

import android.util.DisplayMetrics;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

@Test public void conversions() throws Exception {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();

for (float s = 0; s < 999; s++) {
assertEquals(s, AndroidUnit.PIXELS.toPixels(s), 0);
assertEquals(s / metrics.density, AndroidUnit.PIXELS.toDisplayPixels(s), 0);
assertEquals(s / metrics.scaledDensity, AndroidUnit.PIXELS.toScalePixels(s), 0);

assertEquals(s * metrics.density, AndroidUnit.DENSITY_PIXELS.toPixels(s), 0);
assertEquals(s, AndroidUnit.DENSITY_PIXELS.toDisplayPixels(s), 0);
assertEquals(s * (metrics.density / metrics.scaledDensity),
AndroidUnit.DENSITY_PIXELS.toScalePixels(s), 0);

assertEquals(s * metrics.scaledDensity, AndroidUnit.SCALE_PIXELS.toPixels(s), 0);
assertEquals(s * (metrics.scaledDensity / metrics.density),
AndroidUnit.SCALE_PIXELS.toDisplayPixels(s), 0);
assertEquals(s, AndroidUnit.SCALE_PIXELS.toScalePixels(s), 0);
}
}
}
3 changes: 3 additions & 0 deletions androidunits/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<manifest package="com.github.kevelbreh.androidunits">
<application/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.github.kevelbreh.androidunits;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;

/**
* {@code AndroidUnit} is a utility to convert between the different display units used when laying
* out android views. {@code AndroidUnit} doesn't care about android the {@link Context}.
*/
public enum AndroidUnit {

PIXELS {
@Override public float convert(float sourceCount, AndroidUnit sourceUnit) {
return sourceUnit.toPixels(sourceCount);
}

@Override public float toPixels(float count) {
return count;
}

@Override public float toDisplayPixels(float count) {
return count / DISPLAY_METRICS.density;
}

@Override public float toScalePixels(float count) {
return count / DISPLAY_METRICS.scaledDensity;
}
},

DENSITY_PIXELS {
@Override public float convert(float sourceCount, AndroidUnit sourceUnit) {
return sourceUnit.toDisplayPixels(sourceCount);
}

@Override public float toPixels(float count) {
return count * DISPLAY_METRICS.density;
}

@Override public float toDisplayPixels(float count) {
return count;
}

@Override public float toScalePixels(float count) {
return count * (DISPLAY_METRICS.scaledDensity / DISPLAY_METRICS.density);
}
},

SCALE_PIXELS {
@Override public float convert(float sourceCount, AndroidUnit sourceUnit) {
return sourceUnit.toScalePixels(sourceCount);
}

@Override public float toPixels(float count) {
return count * DISPLAY_METRICS.scaledDensity;
}

@Override public float toDisplayPixels(float count) {
return count * (DISPLAY_METRICS.density / DISPLAY_METRICS.scaledDensity);
}

@Override public float toScalePixels(float count) {
return count;
}
};

private static final DisplayMetrics DISPLAY_METRICS = Resources.getSystem().getDisplayMetrics();

/**
* Converts the given size in the given unit to this unit.
*
* @param sourceCount the size in the given {@code sourceUnit}.
* @param sourceUnit the unit of the {@code sourceCount} argument.
* @return the converted size of the unit
*/
public float convert(float sourceCount, AndroidUnit sourceUnit) {
throw new AbstractMethodError();
}

/**
* Convert from a source unit count to a pixel count
* @param count the source unit count
* @return the converted source to a pixel count
*/
public float toPixels(float count) {
throw new AbstractMethodError();
}

/**
* Convert from a source unit count to a density independent pixel count
* @param count the source unit count
* @return the converted density independent pixel count
*/
public float toDisplayPixels(float count) {
throw new AbstractMethodError();
}

/**
* Convert from a source unit count to a scaled independent pixel count
* @param count the source unit count
* @return the converted scaled independent pixel count
*/
public float toScalePixels(float count) {
throw new AbstractMethodError();
}
}
19 changes: 19 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
17 changes: 17 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Mon Dec 28 10:00:20 PST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Loading

0 comments on commit 12c47c9

Please sign in to comment.