-
-
Save florent37/d792c75bf884e5c7f04ae8c1134f57fd to your computer and use it in GitHub Desktop.
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
/** | |
* Created by McGalanes on 02/12/2016. | |
*/ | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Point; | |
import android.util.DisplayMetrics; | |
import android.view.Display; | |
import android.view.View; | |
import android.view.WindowManager; | |
/** | |
* Cette class met à disposition des fonctions qui sont utiles lors de la création de custom Views | |
*/ | |
public class CustomViewUtils { | |
/** | |
* Permet de déterminer la valeur finale de la largeur de la custom View. | |
* (en fonction de si c'est wrap_content, match_parent etc...) | |
* | |
* @param desiredWidth : la valeur souhaité | |
* @param widthMeasureSpec : le parametre de onMeasure (contient la largeur et le mode) | |
* @return | |
*/ | |
public static int getFinalWidth(int desiredWidth, int widthMeasureSpec){ | |
int widthMode = View.MeasureSpec.getMode(widthMeasureSpec); | |
int widthSize = View.MeasureSpec.getSize(widthMeasureSpec); | |
int finalWidth; | |
if (widthMode == View.MeasureSpec.EXACTLY) { | |
//Must be this size | |
finalWidth = widthSize; | |
} else if (widthMode == View.MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
finalWidth = Math.min(desiredWidth, widthSize); | |
} else { | |
//Be whatever you want | |
finalWidth = desiredWidth; | |
} | |
return finalWidth; | |
} | |
/** | |
* Permet de déterminer la valeur finale de la hauteur de la custom View. | |
* (en fonction de si c'est wrap_content, match_parent etc...) | |
* | |
* @param desiredHeight : la valeur souhaité | |
* @param heightMeasureSpec : le parametre de onMeasure (contient la hauteur et le mode) | |
* @return | |
*/ | |
public static int getFinalHeight(int desiredHeight, int heightMeasureSpec){ | |
int heightMode = View.MeasureSpec.getMode(heightMeasureSpec); | |
int heightSize = View.MeasureSpec.getSize(heightMeasureSpec); | |
int finalHeight; | |
if (heightMode == View.MeasureSpec.EXACTLY) { | |
//Must be this size | |
finalHeight = heightSize; | |
} else if (heightMode == View.MeasureSpec.AT_MOST) { | |
//Can't be bigger than... | |
finalHeight = Math.min(desiredHeight, heightSize); | |
} else { | |
//Be whatever you want | |
finalHeight = desiredHeight; | |
} | |
return finalHeight; | |
} | |
/** | |
* Permet de récupérer la largeur de l'écran en px | |
* | |
* @param context : le context | |
* @return la largeur | |
*/ | |
public static int getScreenWidth(Context context){ | |
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
Display display = wm.getDefaultDisplay(); | |
Point size = new Point(); | |
display.getSize(size); | |
return size.x; | |
} | |
/** | |
* Permet de récupérer la hauteur de l'écran en px | |
* | |
* @param context : le context | |
* @return : la hauteur | |
*/ | |
public static int getScreenHeight(Context context){ | |
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
Display display = wm.getDefaultDisplay(); | |
Point size = new Point(); | |
display.getSize(size); | |
return size.y; | |
} | |
/** | |
* Permet de convertir des dp en px | |
* | |
* @param dp : la valeur en dp | |
* @param context : le context | |
* @return la valeur en px | |
*/ | |
public static float toPx(float dp, Context context){ | |
Resources resources = context.getResources(); | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); | |
return px; | |
} | |
/** | |
* Permet de convertir des px en dp | |
* | |
* @param px : la valeur en px | |
* @param context : le context | |
* @return la valeur en dp | |
*/ | |
public static float toDp(float px, Context context){ | |
Resources resources = context.getResources(); | |
DisplayMetrics metrics = resources.getDisplayMetrics(); | |
float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); | |
return dp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment