Skip to content

Instantly share code, notes, and snippets.

@HeGanjie
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HeGanjie/11032659 to your computer and use it in GitHub Desktop.
Save HeGanjie/11032659 to your computer and use it in GitHub Desktop.
public static float dp2Px(float dp, Context context){
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return dp * (metrics.densityDpi / 160f);
}

public static float px2Dp(float px, Context context) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return px / (metrics.densityDpi / 160f);
}
/**
 * 创建Bundle,暂时只支持 String, Serializable, Int, Boolean, Float, Double, Parcelable
 * @param kvs
 * @return
 */
public static Bundle buildBundle(Object ...kvs) {
	assert(kvs.length % 2 == 0);

	Bundle bundle = new Bundle();
	int i = 0;
	while (i < kvs.length) {
		String key = (String)kvs[i];
		Object value = kvs[i + 1];
		if (value instanceof String) {
			bundle.putString(key, (String)value);
		} else if (value instanceof Integer) {
			bundle.putInt(key, (Integer) value);
		} else if (value instanceof Boolean) {
			bundle.putBoolean(key, (Boolean) value);
		} else if (value instanceof Float) {
			bundle.putFloat(key, (Float) value);
		} else if (value instanceof Double) {
			bundle.putDouble(key, (Double) value);
		} else if (value instanceof Parcelable) {
			bundle.putParcelable(key, (Parcelable) value);
		} else if (value instanceof Serializable) {
			bundle.putSerializable(key, (Serializable) value);
		}
		i += 2;
	}
	return bundle;
}
public static void traceHeapSize() {
	long maxMemory = Runtime.getRuntime().maxMemory();
	trace(NormalUtils.class, "maxMemory(Mb):", Long.toString(maxMemory / 1024 / 1024));
}
public static boolean isSDCardAccessable() {
	return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

public static String getExternalStorageDirPath(Object... dirNames) {
	return CommonUtils.buildString(Environment.getExternalStorageDirectory(),
			File.separator, CommonUtils.displayArray(dirNames, File.separator), File.separator);
}

public static String getStoragePathWithoutSdcard(Context context) {
	return context.getFilesDir().getParent() + File.separator;
}

public static String getStoragePath(Context ctx) {
	if (FileUtil.isSDCardAccessable()) {
		return FileUtil.getExternalStorageDirPath("inforshare");
	} else {
		return FileUtil.getStoragePathWithoutSdcard(ctx);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment