android 中和尺寸相关的工具类(获取屏幕宽高以及dp与xp和sp的转换)

1.因为android中手机型号多种多样,所以有时候需要动态的获取屏幕的宽高

package com.dejun.commonsdk.util;

import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;

/**
 * Author:DoctorWei
 * Time:2018/12/10 11:01
 * Description:获取屏幕相关参数工具类
 * email:[email protected]
 */

public class ScreenUtil {
    /**
     * 获取屏幕的宽
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context){
        DisplayMetrics displayMetrics=context.getResources().getDisplayMetrics();
        if (displayMetrics!=null){
            return displayMetrics.widthPixels;
        }else{
            return 0;
        }
    }

    /**
     * 获取屏幕的高
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context){
        DisplayMetrics displayMetrics=context.getResources().getDisplayMetrics();
        if (displayMetrics!=null){
            return displayMetrics.heightPixels;
        }else{
            return 0;
        }
    }

    /**
     * 判断屏幕是否是竖屏
     * @param context
     * @return
     */
    public static boolean isPortrait(Context context){
        boolean isPortrait=context.getResources().getConfiguration().orientation== Configuration.ORIENTATION_PORTRAIT;
        return isPortrait;
    }

    /**
     * 判断是否是平板
     * @param context
     * @return
     */
    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
}
2.因为android中手机型号多种多样,要把适配的dp或者sp的值转换成代码中能识别的xp

package com.dejun.commonsdk.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;

/**
 * Author:DoctorWei
 * Time:2018/12/10 11:10
 * Description:屏幕尺寸工具类
 * email:[email protected]
 */

public class DensitySizeUtil {
    /**
     * dp转换成像素
     * @param context
     * @param dpValue
     * @return
     */
    public static int dp2px(Context context,int dpValue){
        float density = context.getResources().getDisplayMetrics().density;
        int pxValue= (int) (dpValue*density+0.5f);
        return pxValue;
    }

    /**
     * 像素转换成dp
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context,int pxValue){
        float density=context.getResources().getDisplayMetrics().density;
        int dpValue= (int) (pxValue/density+0.5f);
        return dpValue;
    }
    /**
     * sp转换成像素
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context,int spValue){
        float density = context.getResources().getDisplayMetrics().density;
        int pxValue= (int) (spValue*density+0.5f);
        return pxValue;
    }

    /**
     * 像素转换成sp
     * @param context
     * @param spValue
     * @return
     */
    public static int px2sp(Context context,int spValue){
        float density=context.getResources().getDisplayMetrics().density;
        int dpValue= (int) (spValue/density+0.5f);
        return dpValue;
    }
    /**
     * 可以使用系统的TypeValue来进行转换
     */
    public static int typeValueDp2px(Context context,float dpValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics());
    }
    /**
     * 可以使用系统的TypeValue来进行转换
     */
    public static int typeValueSp2px(Context context,float spValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,context.getResources().getDisplayMetrics());
    }
    /**
     * 可以使用系统的TypeValue来进行转换
     */
    public static int typeValuePx2dp(Context context,float pxValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,pxValue,context.getResources().getDisplayMetrics());
    }
}

猜你喜欢

转载自blog.csdn.net/Anthonybuer/article/details/84937172