Android技术干货Android UI

Android 对UI操作的工具类UIUtils

2016-12-30  本文已影响650人  夜远曦白

Android中对UI操作的工具类,大概还有不少常用方法没有记录进去,以后用到一些再更新就好哒~网上那么多资料来着。

感觉最近简书上关于微信小程序的文章好多哟~ 近来这么火喔~到底该不该去学一下呢?这个问题我要深思一下,有没有时间学啊,这需要自律呀!!!多学点东西总是好的!

我绝对是仙剑是真爱粉呐,今朝眼神好温柔哟~ 今朝和祈好甜蜜啊~羡慕!


截图取自网络
public class UIUtils {

/**
 * @return 应用的上下文
 */
public static Context getContext() {
    return BLEApplication.getInstance();
}

/**
 * 获取资源对象
 */
public static Resources getResources() {
    return getContext().getResources();
}

/**
 * @param id
 * @return 资源文件字符串
 */
public static String getString(int id) {
    return getResources().getString(id);
}

/**
 * @param id
 * @return 资源文件字符串数组
 */
public static String[] getStringArray(int id) {
    return getResources().getStringArray(id);
}

/**
 * @param id
 * @return 资源文件图片
 */
public static Drawable getDrawable(int id) {
    return ContextCompat.getDrawable(getContext(), id);
}

/**
 * @param id
 * @return 资源文件颜色
 */
public static int getColor(int id) {
    return ContextCompat.getColor(getContext(), id);
}

/**
 * @param id
 * @return 颜色的状态选择器
 */
public static ColorStateList getColorStateList(int id) {
    return ContextCompat.getColorStateList(getContext(), id);
}

/**
 * @param id
 * @return 尺寸
 */
public static int getDimen(int id) {
    // 返回具体像素值
    return getResources().getDimensionPixelSize(id);
}

/**
 * dp ->px
 *
 * @param dp
 * @return
 */
public static int dp2px(float dp) {
    float density = getResources().getDisplayMetrics().density;
    return (int) (dp * density + 0.5f);
}

/**
 * px ->dp
 *
 * @param px
 * @return
 */
public static float px2dp(int px) {
    float density = getResources().getDisplayMetrics().density;
    return px / density;
}

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

/**
 * 把自身从父View中移除
 *
 * @param view
 */
public static void removeSelfFromParent(View view) {
    if (view != null) {
        ViewParent parent = view.getParent();
        if (parent != null && parent instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) parent;
            group.removeView(view);
        }
    }
}

/**
 * 请求View树重新布局,用于解决中层View有布局状态而导致上层View状态断裂
 *
 * @param view
 * @param isAll
 */
public static void requestLayoutParent(View view, boolean isAll) {
    ViewParent parent = view.getParent();
    while (parent != null && parent instanceof View) {
        if (!parent.isLayoutRequested()) {
            parent.requestLayout();
            if (!isAll) {
                break;
            }
        }
        parent = parent.getParent();
    }
}

/**
 * 判断触点是否落在该View上
 *
 * @param ev
 * @param v
 * @return
 */
public static boolean isTouchInView(MotionEvent ev, View v) {
    int[] vLoc = new int[2];
    v.getLocationOnScreen(vLoc);
    float motionX = ev.getRawX();
    float motionY = ev.getRawY();
    return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth())
            && motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());
}

/**
 * findViewById的泛型封装,减少强转代码
 *
 * @param layout
 * @param id
 * @param <T>
 * @return
 */
public static <T extends View> T findViewById(View layout, int id) {
    return (T) layout.findViewById(id);
}

/**
 * *获取屏幕的比例
 *
 * @param context *@return
 */
public static float getScaledDensity(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    float value = dm.scaledDensity;
    return value;
}

/**
 * 获取控件的高度,如果获取的高度为0,则重新计算尺寸后再返回高度
 *
 * @param view
 * @return
 */
public static int getViewMeasuredHeight(View view) {
    calcViewMeasure(view);
    return view.getMeasuredHeight();
}

/**
 * 获取控件的宽度,如果获取的宽度为0,则重新计算尺寸后再返回宽度
 *
 * @param view
 * @return
 */
public static int getViewMeasuredWidth(View view) {
    calcViewMeasure(view);
    return view.getMeasuredWidth();
}

/**
 * 测量控件的尺寸
 *
 * @param view
 */
public static void calcViewMeasure(View view) {
    int width = View.MeasureSpec.makeMeasureSpec(0,
            View.MeasureSpec.UNSPECIFIED);
    int expandSpec = View.MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
    view.measure(width, expandSpec);
}

/**
 * 设置textview指定文字为某一颜色
 *
 * @param content 显示的文字
 * @param color   需要转换成的颜色值
 * @param start   需要变色文字开始位置
 * @param end     需要变色文字结束位置
 */
public static SpannableStringBuilder changeTextColor(String content, int color, int start, int end) {
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
    spannableStringBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannableStringBuilder;
}}

好啦~今天公司年会活动,上午正常上班,下午活动,年会咋不是一天捏,哈哈,小公司嘛,年会做活动用不了那么长时间的啦!

上一篇下一篇

猜你喜欢

热点阅读