2013-03-13 88 views
0

我有一个小问题,如在这张照片中显示的那样,我无法恢复屏幕大小,因为我想要的,我有点失落... (我正在学习)无法取回屏幕大小android

http://imageshack.us/f/708/erreurtaillepong4.png/

http://imageshack.us/f/708/erreurtaillepong5.png/

我需要有屏幕的宽度和高度

请帮

编辑1

这里是我的代码问:

package com.salocincreations.pong; 

import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 

public class GameState { 

    //Largeur et hauteur de l'écran 
    int _screenWidth = TailleEcran.Measuredwidth; 
    int _screenHeight = TailleEcran.Measuredheight; 


    //La balle 
    final int _ballSize = 10; 
    int _ballX = _screenWidth/2; int _ballY = _screenHeight/2; 
    int _ballVelocityX = 2;  int _ballVelocityY = 4; 

    //Les barres 
    final int _batLength = 75; final int _batHeight = 10; 
    int _topBatX = (_screenWidth/2) - (_batLength/2); 
    final int _topBatY = 10; 
    int _bottomBatX = (_screenWidth/2) - (_batLength/2); 
    final int _bottomBatY = _screenHeight - 20; 

    public GameState() 
    { 
    } 

    //The update method 
    public void update() { 

    _ballX += _ballVelocityX; 
    _ballY += _ballVelocityY; 

    //DEATH! 
    if(_ballY > _bottomBatY + 10 || _ballY < 0)   
    {_ballX = 100; _ballY = 100;}//Collisions with the goals 

    if(_ballX > _screenWidth || _ballX < 0) 
       _ballVelocityX *= -1; //Collisions with the sides  

    if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY - 16 < _topBatY)   
        _ballVelocityY *= -1; //Collisions with the bats  

    if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength 
        && _ballY + 16 > _bottomBatY) 
          _ballVelocityY *= -1; 
    } 

    public boolean surfaceTouched(float posX, float posY) { 
     _topBatX = (int) posX; 
     _bottomBatX = (int) posX; 

     return true; 
     } 


    //the draw method 
    public void draw(Canvas canvas, Paint paint) { 

    //Clear the screen 
    canvas.drawRGB(0, 0, 0); 

    //set the colour 
    paint.setARGB(200, 0, 200, 700); 

    //draw the ball 
    canvas.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), 
           paint); 

    //draw the bats 
    canvas.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, 
              _topBatY + _batHeight), paint); //top bat 
    canvas.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, 
              _bottomBatY + _batHeight), paint); //bottom bat 

     // Nous allons dessiner nos points par rapport à la résolution de l'écran 
     int iWidth = canvas.getWidth(); // Largeur 
     int iHeight = canvas.getHeight(); // Hauteur 

     // Affecter une couleur de manière aléatoire 
      paint.setARGB(255, 500, 500, 500); 
      // Définir l'épaisseur du segment 
      paint.setStrokeWidth (2); 
      // Puis dessiner nos points dans le cavenas 
      canvas.drawLine(0, iHeight/2, iWidth, iHeight/2, paint);  
      canvas.drawCircle(iWidth/2, iHeight/2, 50, paint); 
     }    
    } 

public class TailleEcran extends Activity { 


    int Measuredwidth; 
    int Measuredheight; 
    Point size = new Point(); 
    WindowManager w = getWindowManager();{ 

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){ 
      w.getDefaultDisplay().getSize(size); 

      Measuredwidth = size.x; 
      Measuredheight = size.y; 
      }else{ 
      Display d = w.getDefaultDisplay(); 
      Measuredwidth = d.getWidth(); 
      Measuredheight = d.getHeight(); 
      }}} 

在线路

int _screenWidth = TailleEcran.Measuredwidth; 
int _screenHeight = TailleEcran.Measuredheight; 

错误说:不能让一个静态参照非静态字段TailleEcran.Measuredwidth

不能使静态参考非静态字段TailleEcran.Measuredheight

EDIT 2

然后,anthropomo,我需要写?

 public class GameState { 
     // declare variables above here without assignments 
     Display display = ((WindowManager) 
      context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     DisplayMetrics metrics = new DisplayMetrics(); 
     display.getMetrics(metrics); 

     int pixHeight = metrics.heightPixels; 
     int pixWidth = metrics.widthPixels; 
    // dp numbers: 
     float density = context.getResources().getDisplayMetrics().density; 
     float dpHeight = pixHeight/density; 
     float dpWidth = pixWidth/density; 
public GameState(Context context){ 
//all the rest of my class 
    } 

} 
+0

见http://stackoverflow.com/questions/15366712/size-of-the-usable-screen/15367869#15367869了解视图大小的标准方法。 – 2013-03-13 16:17:32

+0

而不是屏幕截图,您应该将相关代码部分剪切并粘贴到您的问题中。 – anthropomo 2013-03-13 16:22:00

回答

0

试试这个

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

在API 13,然后才能使用:

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

看看我的第二个链接,我已经在另一个班级,但我认为它返回0 – NicMer 2013-03-13 16:12:05

+0

而我有多个错误外观http://imageshack.us/f/803/erreurtaillepong6.png/ – NicMer 2013-03-13 16:17:49

+0

@NicMer请贴你的代码在你的问题我会尝试调试它。 – 2013-03-13 16:19:41

0

您可以使用Context.getResources()Resources.getDisplayMetrics这样的:

DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); 

然后你”哈哈已经

displayMetrics.widthPixels; // width 
displayMetrics.heightPixels; // height 
+0

我的课程必须扩展什么?当我把“扩展活动”,我有多个错误看http://imageshack.us/f/547/erreurtaillepong7.png/ – NicMer 2013-03-13 16:20:09

0

对于兼容性< 13和非弃用:

Display display = ((WindowManager) 
     context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    display.getMetrics(metrics); 

    int pixHeight = metrics.heightPixels; 
    int pixWidth = metrics.widthPixels; 
// dp numbers: 
    float density = context.getResources().getDisplayMetrics().density; 
    float dpHeight = pixHeight/density; 
    float dpWidth = pixWidth/density; 

编辑:于display.getMetrics(metrics);

你得到错误,因为只有声明可以发生在一个类的方法之外。所有这些代码应该在一个方法中,并假设GameState不是一个真正的Activity,这个东西应该在构造函数中。事情是这样的:

public class GameState { 
    // declare variables above here without assignments 

    public GameState(Context context){ 
     // everything above, but save the variables outside of the constructor 
    } 

} 

然后在使用游戏状态的活动,做gameState = new GameState(this);

+0

我有太多的错误:“上下文无法解决”,看看http:// imageshack。 us/f/854/erreurtaillepong8.png/ – NicMer 2013-03-13 16:23:37

+0

在这种情况下,上下文将是'this',如果它在您的活动中。 – anthropomo 2013-03-13 16:25:00

+0

好的,上下文错误消失,但display.getMetrics(度量)仍然存在错误; – NicMer 2013-03-13 16:27:58