获取屏幕宽高,dp转px

public int getScreenHeight() {
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);


int width = metric.widthPixels; // 屏幕宽度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
System.out.println(height / density);
return (int) (height / density);
}


/**
* dip/dp转像素

* @param context
*            context
* @param dipValue
*            dip或 dp大小
* @return 像素值
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}

你可能感兴趣的:(获取屏幕宽高,dp转px)