黄家琛 2019-07-21 10:30 采纳率: 60%
浏览 694
已采纳

安卓关于UIUtils工具类的使用

小弟最近项目用到了一个UIUtils的UI工具类,请问这个类是要自己进行添加的吗?
如果是的话,我找了几篇大神写的帖子,其中有个地方不是很懂:(代码如下)

/**
* @return 应用的上下文
*/
public static Context getContext() {
return xxxApplication.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;
}


    上面在一开始获取上下文时有一个 return xxxApplication.getInstance();
    这个xxxApplication是指我当前应用的名字吗,应该怎么知道,我已经把握觉得可能的名字打进去了都报红
  • 写回答

2条回答 默认 最新

  • 代码的灵魂是bug! 2019-07-22 09:21
    关注

    xxxApplication也可能是需要你自己新建一个类并继承Application

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条