package com.criuse.tan.util;

import android.app.Activity;
import android.util.DisplayMetrics;

/**
 * Created by zhiqiang on 8/24/15.
 */
public class DisplayUtil {
    public static final int COMPLEX_UNIT_PX = 0;
    public static final int COMPLEX_UNIT_DIP = 1;
    public static final int COMPLEX_UNIT_SP = 2;
    public static final int COMPLEX_UNIT_PT = 3;
    public static final int COMPLEX_UNIT_IN = 4;
    public static final int COMPLEX_UNIT_MM = 5;
    private static DisplayMetrics dm;

    /**
     * dip 2 px
     *
     * @param activity
     * @param dip
     * @return px value
     */
    public static float getDip2Px(Activity activity, float dip) {
        initDm(activity);
        return applyDimension(COMPLEX_UNIT_DIP, dip, dm);
    }

    /**
     * px 2 dip
     *
     * @param activity
     * @param px
     * @return
     */
    public static float getPx2Dip(Activity activity, float px) {
        return px / dm.density;
    }

    private static void initDm(Activity activity) {
        if (dm == null) {
            dm = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        }
    }

    public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
        switch (unit) {
            case COMPLEX_UNIT_PX:
                return value;
            case COMPLEX_UNIT_DIP:
                return value * metrics.density;
            case COMPLEX_UNIT_SP:
                return value * metrics.scaledDensity;
            case COMPLEX_UNIT_PT:
                return value * metrics.xdpi * (1.0f / 72);
            case COMPLEX_UNIT_IN:
                return value * metrics.xdpi;
            case COMPLEX_UNIT_MM:
                return value * metrics.xdpi * (1.0f / 25.4f);
        }
        return 0;
    }
}

@@ -183,32 +183,24 @@
 c final int 
-COMPLEX_
 UNIT_PX = 0;
@@ -224,32 +224,24 @@
 c final int 
-COMPLEX_
 UNIT_DIP = 1
@@ -266,32 +266,24 @@
 c final int 
-COMPLEX_
 UNIT_SP = 2;
@@ -307,32 +307,24 @@
 c final int 
-COMPLEX_
 UNIT_PT = 3;
@@ -348,32 +348,24 @@
 c final int 
-COMPLEX_
 UNIT_IN = 4;
@@ -393,24 +393,16 @@
 nal int 
-COMPLEX_
 UNIT_MM 
@@ -671,24 +671,16 @@
 mension(
-COMPLEX_
 UNIT_DIP
@@ -853,24 +853,50 @@
 float px) %7B%0A
+        initDm(activity);%0A
         retu
@@ -1257,32 +1257,24 @@
        case 
-COMPLEX_
 UNIT_PX:%0A   
@@ -1313,32 +1313,24 @@
        case 
-COMPLEX_
 UNIT_DIP:%0A  
@@ -1388,32 +1388,24 @@
        case 
-COMPLEX_
 UNIT_SP:%0A   
@@ -1468,32 +1468,24 @@
        case 
-COMPLEX_
 UNIT_PT:%0A   
@@ -1553,32 +1553,24 @@
        case 
-COMPLEX_
 UNIT_IN:%0A   
@@ -1632,16 +1632,8 @@
 ase 
-COMPLEX_
 UNIT

replace COMPLEX_