工具类 dp转px 获取图片实际尺寸 获取屏幕尺寸

时间:2021-07-22 02:47:35

dp转px

public class Dp2pxUtils {
public static int Dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * 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);
}
}

获取图片实际尺寸

public class ImgUtils {

    /**
* 用bitmap获取图片的本来宽高
*/
public static int getImgOriginalWidth(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true,表示解析Bitmap对象,该对象不占内存
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); int bitmapWidth = options.outWidth; return bitmapWidth;
}
/**
* 用bitmap获取图片的本来宽高
*/
public static int getImgOriginalHeight(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true,表示解析Bitmap对象,该对象不占内存
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); int bitmapHeight = options.outHeight; return bitmapHeight;
}
}
public class ImgUtils {

    /**
* 用bitmap获取图片的本来宽高
*/
public static int getImgOriginalWidth(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true,表示解析Bitmap对象,该对象不占内存
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); int bitmapWidth = options.outWidth; return bitmapWidth;
}
/**
* 用bitmap获取图片的本来宽高
*/
public static int getImgOriginalHeight(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
//设置为true,表示解析Bitmap对象,该对象不占内存
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options); int bitmapHeight = options.outHeight; return bitmapHeight;
}
}

获取屏幕尺寸

public class ScreenUtil {

    public static int getScreenHight(Context context) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.heightPixels;
} public static int getScreenWidth(Context context) {
DisplayMetrics displaymetrics = new DisplayMetrics();
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displaymetrics);
return displaymetrics.widthPixels;
}
}