获取屏幕宽度和高度

我怎么能得到屏幕的宽度和高度来使用这个值

@Override protected void onMeasure(int widthSpecId, int heightSpecId) { Log.e(TAG, "onMeasure" + widthSpecId); setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT - game.findViewById(R.id.flag).getHeight()); } 

使用这个代码,你可以得到运行时Display's Width & Height

 DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; int width = displayMetrics.widthPixels; 

在一个视图中,你需要做这样的事情:

 ((Activity) getContext()).getWindowManager() .getDefaultDisplay() .getMetrics(displayMetrics); 

有一个非常简单的答案,没有通过上下文

 public static int getScreenWidth() { return Resources.getSystem().getDisplayMetrics().widthPixels; } public static int getScreenHeight() { return Resources.getSystem().getDisplayMetrics().heightPixels; } 

结果和@Parag Chauhan的答案一样

只需要通过parag和SpK来更新答案,以便与已弃用方法中的当前SDK向后兼容性保持一致:

 int Measuredwidth = 0; int Measuredheight = 0; Point size = new Point(); WindowManager w = getWindowManager(); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { w.getDefaultDisplay().getSize(size); Measuredwidth = size.x; Measuredheight = size.y; }else{ Display d = w.getDefaultDisplay(); Measuredwidth = d.getWidth(); Measuredheight = d.getHeight(); } 

为什么不

DisplayMetrics displaymetrics = getResources()。getDisplayMetrics();

然后使用

displayMetrics.widthPixels(heightPixels)

尝试下面的代码: –

1。

 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; 

2。

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); // deprecated int height = display.getHeight(); // deprecated 

要么

 int width = getWindowManager().getDefaultDisplay().getWidth(); int height = getWindowManager().getDefaultDisplay().getHeight(); 

3。

 DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); metrics.heightPixels; metrics.widthPixels; 
 DisplayMetrics dimension = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dimension); int width = dimension.widthPixels; int height = dimension.heightPixels; 

//获取屏幕宽度和高度的值

  Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); width = size.x; height = size.y; 

这里显示的方法已被弃用/过时,但仍然有效。需要API 13

一探究竟

 Display disp= getWindowManager().getDefaultDisplay(); Point dimensions = new Point(); disp.getSize(size); int width = size.x; int height = size.y; 
 DisplayMetrics lDisplayMetrics = getResources().getDisplayMetrics(); int widthPixels = lDisplayMetrics.widthPixels; int heightPixels = lDisplayMetrics.heightPixels; 
 public class DisplayInfo { int screen_height=0, screen_width=0; WindowManager wm; DisplayMetrics displaymetrics; DisplayInfo(Context context) { getdisplayheightWidth(context); } void getdisplayheightWidth(Context context) { wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); displaymetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(displaymetrics); screen_height = displaymetrics.heightPixels; screen_width = displaymetrics.widthPixels; } public int getScreen_height() { return screen_height; } public int getScreen_width() { return screen_width; } 

}

 Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int mWidthScreen = display.getWidth(); int mHeightScreen = display.getHeight(); 

只需使用下面的函数作为整数数组返回屏幕大小的宽度和高度

 private int[] getScreenSIze(){ DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int h = displaymetrics.heightPixels; int w = displaymetrics.widthPixels; int[] size={w,h}; return size; } 

在您的onCreatefunction或button上单击添加下面的代码来输出如下所示的屏幕大小

  int[] screenSize= getScreenSIze(); int width=screenSize[0]; int height=screenSize[1]; screenSizes.setText("Phone Screen sizes \n\n width = "+width+" \n Height = "+height); 
  @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); }