我如何获得屏幕的宽度和高度,并使用这个值在:
@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
Log.e(TAG, "onMeasure" + widthSpecId);
setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT -
game.findViewById(R.id.flag).getHeight());
}
我如何获得屏幕的宽度和高度,并使用这个值在:
@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
Log.e(TAG, "onMeasure" + widthSpecId);
setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT -
game.findViewById(R.id.flag).getHeight());
}
当前回答
@Override
protected void onPostExecute(Drawable result) {
Log.d("width",""+result.getIntrinsicWidth());
urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 600);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((400+URLImageParser.this.container.getHeight()
+ result.getIntrinsicHeight()));
// Pre ICS`enter code here`
URLImageParser.this.container.setEllipsize(null);
}
其他回答
DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics();
int widthPixels = lDisplayMetrics.widthPixels;
int heightPixels = lDisplayMetrics.heightPixels;
有一个非常简单的答案,没有通过上下文
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
注:如果你想要高度包括导航条,使用下面的方法
WindowManager windowManager =
(WindowManager) BaseApplication.getApplication().getSystemService(Context.WINDOW_SERVICE);
final Display display = windowManager.getDefaultDisplay();
Point outPoint = new Point();
if (Build.VERSION.SDK_INT >= 19) {
// include navigation bar
display.getRealSize(outPoint);
} else {
// exclude navigation bar
display.getSize(outPoint);
}
if (outPoint.y > outPoint.x) {
mRealSizeHeight = outPoint.y;
mRealSizeWidth = outPoint.x;
} else {
mRealSizeHeight = outPoint.x;
mRealSizeWidth = outPoint.y;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static double getHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getRealMetrics(displayMetrics);
//int height = displayMetrics.heightPixels;
//int width = displayMetrics.widthPixels;
return displayMetrics.heightPixels;
}
使用这种方法你可以得到屏幕高度。 如果你想要宽度改变displayMetrics。hightpixels到displayMetrics.widthPixels。
它还包括所需的api构建版本。
屏幕分辨率是屏幕中像素的总和。下面的程序将提取设备的屏幕分辨率。它将打印屏幕的宽度和高度。这些值以像素为单位。
public static Point getScreenResolution(Context context) {
// get window managers
WindowManager manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);
// get width and height
int width = point.x;
int height = point.y;
return point;
}
@Override
protected void onPostExecute(Drawable result) {
Log.d("width",""+result.getIntrinsicWidth());
urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 600);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((400+URLImageParser.this.container.getHeight()
+ result.getIntrinsicHeight()));
// Pre ICS`enter code here`
URLImageParser.this.container.setEllipsize(null);
}