惠存实用的屏幕计算工具类

public class UIUtil {

public static DisplayMetrics getDisplayMetrics(Context context) {
    return context.getResources().getDisplayMetrics();
}

public static int px2sp(Context context, float pxValue) {
    DisplayMetrics displayMetrics = getDisplayMetrics(context);
    return (int) (pxValue / displayMetrics.scaledDensity + 0.5f);
}

public static int sp2px(Context context, float spValue) {
    DisplayMetrics displayMetrics = getDisplayMetrics(context);
    return (int) (spValue * displayMetrics.scaledDensity + 0.5f);
}

public static int dp2px(Context context, float dp) {
    DisplayMetrics displayMetrics = getDisplayMetrics(context);
    return (int) (dp * displayMetrics.density + 0.5);
}

public static int px2dp(Context context, float dp) {
    DisplayMetrics displayMetrics = context.getResources()
            .getDisplayMetrics();
    return (int) (dp / displayMetrics.density + 0.5);
}

/**
 * 获取通知栏高度
 *
 * @Title: getBarHeight
 * @param @param context
 * @param @return 设定文件
 * @return int 返回类型
 */
private static int sStatusBarHeight = 0;

public static int getStatusBarHeight(Context context) {
    if (sStatusBarHeight <= 0) {
        Class<?> c     = null;
        Object   obj   = null;
        Field    field = null;
        int      x     = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
        }
    }
    return sStatusBarHeight;
}

/**
 * @param root 根视图
 * @param size 字体大小
 */
// 修改整个界面所有控件的字体大小
public static void changeTextSize(ViewGroup root, int size) {
    for (int i = 0; i < root.getChildCount(); i++) {
        View v = root.getChildAt(i);
        if (v instanceof TextView) {
            ((TextView) v).setTextSize(size);
        } else if (v instanceof ViewGroup) {
            changeTextSize((ViewGroup) v, size);
        }
    }
}

public static void measureView(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    }
    int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
                MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}

public static boolean isTouchView(View view, MotionEvent event) {
    if (view != null) {
        int[] leftTop = {0, 0};
        view.getLocationInWindow(leftTop);
        int top = leftTop[1], bottom = top + view.getHeight();
        if (event.getY() > top && event.getY() < bottom) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

/**
 * 5.0以上版本手机动态设置通知栏颜色的方法
 *
 * @param activity 需要设置颜色的Activity
 * @param colorRes 设置的颜色
 */
public static void setStatusBarColor(Activity activity, @ColorRes int colorRes) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int statusColor = activity.getResources().getColor(colorRes);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        activity.getWindow().setStatusBarColor(statusColor);
    }
}

public void hiddenView() {
    AnimationSet       animationSet  = new AnimationSet(true);
    TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
    mHiddenAction.setDuration(100);
    mHiddenAction.setFillAfter(true);
    animationSet.addAnimation(mHiddenAction);
}

public void showView() {
    AnimationSet       animationSet = new AnimationSet(true);
    TranslateAnimation mShowAction  = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);//-1
    mShowAction.setDuration(100);
    mShowAction.setFillAfter(true);
    animationSet.addAnimation(mShowAction);
}

/**
 * 设置视图的偏移量
 * @param v
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
public static void setMargins (View v, int left, int top, int right, int bottom) {
    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        v.requestLayout();
    }
}

// 成新的颜色值
public static int getNewColorByStartEndColor(Context context, float fraction, int startValue, int endValue) {
    return evaluate(fraction, context.getResources().getColor(startValue), context.getResources().getColor(endValue));
}
/**
 * 成新的颜色值
 * @param fraction 颜色取值的级别 (0.0f ~ 1.0f)
 * @param startValue 开始显示的颜色
 * @param endValue 结束显示的颜色
 * @return 返回生成新的颜色值
 */
public static int evaluate(float fraction, int startValue, int endValue) {
    int startA = (startValue >> 24) & 0xff;
    int startR = (startValue >> 16) & 0xff;
    int startG = (startValue >> 8) & 0xff;
    int startB = startValue & 0xff;

    int endA = (endValue >> 24) & 0xff;
    int endR = (endValue >> 16) & 0xff;
    int endG = (endValue >> 8) & 0xff;
    int endB = endValue & 0xff;

    return ((startA + (int) (fraction * (endA - startA))) << 24) |
            ((startR + (int) (fraction * (endR - startR))) << 16) |
            ((startG + (int) (fraction * (endG - startG))) << 8) |
            ((startB + (int) (fraction * (endB - startB))));
}
// 根据手机的分辨率将dp的单位转成px(像素)
public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

// 根据手机的分辨率将px(像素)的单位转成dp
public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值