亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

項目框架工具類(UIUtils)

標簽:
Android
/**
 * 工具类, 专门用来获取UI相关需要的参数
 * 
 * @author Kevin
 * 
 */
public class UIUtils {

public static Context getContext() {
    return BaseApplication.getContext();//BaseApplication为我们自定义的Application,里面初始化了一些静态全局变量!
}

public static int getMainThreadId() {
    return BaseApplication.getMainThreadId();
}

public static Handler getHandler() {
    return BaseApplication.getHandler();
}

/**
 * 根据id获取字符串
 */
public static String getString(int id) {
    return getContext().getResources().getString(id);
}

/**
 * 根据id获取图片
 */
public static Drawable getDrawable(int id) {
    return getContext().getResources().getDrawable(id);
}

/**
 * 根据id获取颜色值
 */
public static int getColor(int id) {
    return getContext().getResources().getColor(id);
}

/**
 * 获取颜色状态集合
 */
public static ColorStateList getColorStateList(int id) {
    return getContext().getResources().getColorStateList(id);
}

/**
 * 根据id获取尺寸
 */
public static int getDimen(int id) {
    return getContext().getResources().getDimensionPixelSize(id);
}

/**
 * 根据id获取字符串数组
 */
public static String[] getStringArray(int id) {
    return getContext().getResources().getStringArray(id);
}

/**
 * dp转px
 */
public static int dip2px(float dp) {
    float density = getContext().getResources().getDisplayMetrics().density;
    return (int) (density * dp + 0.5);
}

/**
 * px转dp
 */
public static float px2dip(float px) {
    float density = getContext().getResources().getDisplayMetrics().density;
    return px / density;
}

/**
 * 加载布局文件
 */
public static View inflate(int layoutId) {
    return View.inflate(getContext(), layoutId, null);
}

/**
 * 判断当前是否运行在主线程
 * 
 * @return
 */
public static boolean isRunOnUiThread() {
    return getMainThreadId() == android.os.Process.myTid();
}

/**
 * 保证当前的操作运行在UI主线程
 * 
 * @param runnable
 */
public static void runOnUiThread(Runnable runnable) {    
    if (isRunOnUiThread()) {
        runnable.run();
        } else {
            getHandler().post(runnable);
        }
    }
}
點擊查看更多內容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消