-
-
Save amitkot/160c5caa48d62a86ea93a3141ef41ac3 to your computer and use it in GitHub Desktop.
A small utility to help converting between dips and pixels in your Android code. Most solutions are unreadable, this one should read a lot better.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Resources; | |
import android.support.annotation.NonNull; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
/** | |
* Convert between DP and pixels, like {@link java.util.concurrent.TimeUnit} does. | |
* For example: | |
* Dimens.DP.toPX(context.getResources(), 16); // 16dp to pixels | |
* Dimens.PX.toDP(context.getResources(), 16); // 16px to dips | |
*/ | |
public enum Dimens { | |
DP { | |
public int toPX(@NonNull final Resources resources, int dp) { | |
return (int) TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); | |
} | |
public int toDP(@NonNull final Resources resources, int dp) { | |
return dp; | |
} | |
}, | |
PX { | |
public int toPX(@NonNull final Resources resources, int px) { | |
return px; | |
} | |
public int toDP(@NonNull final Resources resources, int px) { | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
return (int) (px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)); | |
} | |
}; | |
Dimens() { | |
} | |
public int toPX(@NonNull final Resources resources, int value) { | |
throw new AbstractMethodError(); | |
} | |
public int toDP(@NonNull final Resources resources, int value) { | |
throw new AbstractMethodError(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Resources; | |
import android.support.annotation.NonNull; | |
import android.util.DisplayMetrics; | |
import android.util.TypedValue; | |
/** | |
* Convert between DP and pixels, like {@link java.util.concurrent.TimeUnit} does. | |
* But now without enums, because #perfmatters | |
* For example: | |
* Dimens2.DP.toPX(context.getResources(), 16); // 16dp to pixels | |
* Dimens2.PX.toDP(context.getResources(), 16); // 16px to dips | |
*/ | |
public class Dimens2 { | |
public static class DP { | |
DP() { | |
} | |
public static float toPX(@NonNull final Resources resources, int dp) { | |
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); | |
} | |
public static float toDP(@NonNull final Resources resources, int dp) { | |
return dp; | |
} | |
} | |
public static class PX { | |
PX() { | |
} | |
public static float toPX(@NonNull final Resources resources, int px) { | |
return px; | |
} | |
public static float toDP(@NonNull final Resources resources, int px) { | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
return px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); | |
} | |
} | |
Dimens() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment