Comuilクラス

17149 ワード

/**
 * Created by Dash
 */
public class CommonUtils {
    public static final String TAG = "Dash";//sp   xml  
    private static SharedPreferences sharedPreferences;

    /**
     * DashApplication.getAppContext()    ,     * @param layoutId
     * @return
     */
    public static View inflate(int layoutId) {
        View view = View.inflate(getAppContext(), layoutId, null);
        return view;
    }

    /**
     * dip---px
     *
     * @param dip       device independent px....1dp = 3px 1dp = 2px 1dp = 1.5px
     * @return
     */
    public static int dip2px(int dip) {
        //      
        float density = getAppContext().getResources().getDisplayMetrics().density;
        //
        int px = (int) (dip * density + 0.5f);//100.6
        return px;

    }

    /**
     * px-dip
     *
     * @param px
     * @return
     */
    public static int px2dip(int px) {
        //      
        float density = getAppContext().getResources().getDisplayMetrics().density;
        //
        int dip = (int) (px / density + 0.5f);
        return dip;

    }

    /**
     *          
     * @param stringId
     * @return
     */
    public static String getString(int stringId) {
        return getAppContext().getResources().getString(stringId);
    }

    public static Drawable getDrawable(int did) {
        return getAppContext().getResources().getDrawable(did);
    }

    public static int getDimens(int id) {
        return getAppContext().getResources().getDimensionPixelSize(id);
    }

    /**
     * sp         
     * @param flag
     * @param str
     */
    public static void saveSp(String flag, String str) {
        if (sharedPreferences == null) {
            sharedPreferences = getAppContext().getSharedPreferences(TAG, getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(flag, str);
        edit.commit();
    }

    public static String getSp(String flag) {
        if (sharedPreferences == null) {
            sharedPreferences = getAppContext().getSharedPreferences(TAG, getAppContext().MODE_PRIVATE);
        }
        return sharedPreferences.getString(flag, "");
    }

    public static boolean getBoolean(String tag) {
        if (sharedPreferences == null) {
            sharedPreferences = getAppContext().getSharedPreferences(TAG, getAppContext().MODE_PRIVATE);
        }
        return sharedPreferences.getBoolean(tag, false);
    }

    public static void putBoolean(String tag, boolean content) {
        if (sharedPreferences == null) {
            sharedPreferences = getAppContext().getSharedPreferences(TAG, getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(tag, content);
        edit.commit();
    }

    /**
     *   sp  
     * @param tag
     */
    public static void clearSp(String tag) {
        if (sharedPreferences == null) {
            sharedPreferences = getAppContext().getSharedPreferences(TAG, getAppContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.remove(tag);
        edit.commit();
    }

    /**
     *              
     *       ,    ,    handler          
     *
     *
     * @param runable
     */
    public static void runOnUIThread(Runnable runable) {
        //               
        if (android.os.Process.myTid() == Myapplication.getMainThreadId()) {
            runable.run();
        } else {
            //   
            Myapplication.getAppHanler().post(runable);
        }
    }
}