Created
August 13, 2016 05:08
-
-
Save olkunmustafa/f315c60f0606241bec6b8b7f6c4c63a8 to your computer and use it in GitHub Desktop.
Uses to convert to px from dip or vice versa.
This file contains 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
package com.fallavi.android.utils; | |
import android.content.res.Resources; | |
import android.util.DisplayMetrics; | |
/** | |
* Created by olkunmustafa on 13/08/16 | |
* | |
* Gives dimensions according to device's denisty | |
*/ | |
public class DimensionHelper { | |
/** | |
* This method converts dp unit to equivalent pixels, depending on device density. | |
* | |
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels | |
* @return A float value to represent px equivalent to dp depending on device density | |
* @since 1.3.16 | |
*/ | |
public static int convertDpToPixel( float dp ) { | |
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); | |
return ( int ) ( dp * ( metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT ) ); | |
} | |
/** | |
* This method converts device specific pixels to density independent pixels. | |
* | |
* @param px A value in px (pixels) unit. Which we need to convert into db | |
* @return A float value to represent dp equivalent to px value | |
* @since 1.3.16 | |
*/ | |
public static int convertPixelsToDp( float px ) { | |
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); | |
return ( int ) ( px / ( metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment