public class UIUtils {
public static Context getContext() {
return Application.getContext();
}
public static Handler getHandler() {
return Application.getHandler();
}
public static int getMainThreadId() {
return Application.getMainThreadId();
}
public static String getString(int id) {
return getContext().getResources().getString(id);
}
public static String[] getStringArray(int id) {
return getContext().getResources().getStringArray(id);
}
public static Drawable getDrawable(int id) {
return getContext().getResources().getDrawable(id);
}
public static int getColor(int id) {
return getContext().getResources().getColor(id);
}
public static ColorStateList getColorStateList(int id) {
return getContext().getResources().getColorStateList(id);
}
public static int getDimen(int id) {
return getContext().getResources().getDimensionPixelSize(id);
}
public static int dip2px(float dip) {
float density = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * density + 0.5f);
}
public static float px2dip(int px) {
float density = getContext().getResources().getDisplayMetrics().density;
return px / density;
}
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
public static boolean isRunOnUIThread() {
int myTid = android.os.Process.myTid();
if (myTid == getMainThreadId()) {
return true;
}
return false;
}
public static void runOnUIThread(Runnable r) {
if (isRunOnUIThread()) {
r.run();
} else {
getHandler().post(r);
}
}
}