package com.yalantis.ucrop.util; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; /** * author:luck * project:LoveCare * package:com.tongyu.luck.lovecare.util * email:893855882@qq.com * data:16/8/10 */ public class Utils { /** * 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 * @param context Context to get resources and device specific display metrics * @return A float value to represent px equivalent to dp depending on device density */ public static float convertDpToPixel(float dp, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f); return px; } /** * 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 * @param context Context to get resources and device specific display metrics * @return A float value to represent dp equivalent to px value */ public static float convertPixelsToDp(float px, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float dp = px / (metrics.densityDpi / 160f); return dp; } /** * dpתpx */ public static int dip2px(Context ctx, float dpValue) { final float scale = ctx.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * pxתdp */ public static int px2dip(Context ctx, float pxValue) { final float scale = ctx.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 防止连续点击跳转两个页面 */ public static long lastClickTime; public static boolean isFastDoubleClick() { long time = System.currentTimeMillis(); if (time - lastClickTime < 800) { return true; } lastClickTime = time; return false; } /** * 防止连续点击跳转两个页面 */ public static long lastClickTime2; public static boolean isFastDoubleClick2() { long time = System.currentTimeMillis(); if (time - lastClickTime < 800) { return true; } lastClickTime2 = time; return false; } /** * 判断 一个字段的值否为空 * * @param s * @return * @author Michael.Zhang 2013-9-7 下午4:39:00 */ public static boolean isNull(String s) { if (null == s || s.equals("") || s.equalsIgnoreCase("null")) { return true; } return false; } }