Android onMeasure()测量宽度但不测量高度

时间:2012-11-10 18:18:47

标签: android view size measure

我有一个SurfaceView,我需要知道它的宽度和高度,代码如下:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    screenWidth = MeasureSpec.getSize(widthMeasureSpec);
    screenHeight = MeasureSpec.getSize(heightMeasureSpec);
}

由于某种原因它设法测量这个视图的宽度而不是它的高度,我已经尝试将高度测量放在宽度之前,看看它是否只是没有足够快地调用onMeasure()方法但是它没用。

编辑:

下面是所有代码:

public class GameView extends SurfaceView {
GameLoopThread gameLoopThread;
Integer screenWidth, screenHeight;
static Integer score;
long lastTime = 0;
byte speed = 5;

static Boolean mute;
static String textureUsed = "space";

public GameView(Context context) {
    super(context);
    mute = MainMenu.getMute();
    score = 0;
    gameLoopThread = new GameLoopThread(this);
    SurfaceHolder holder = getHolder();
    holder.addCallback(new Callback() {

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }

        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }
    });
}

// graphics are drawn here
@Override
protected void onDraw(Canvas canvas) {
    //covers the whole canvas in white to clear previouslly draw graphics
    canvas.drawColor(Color.WHITE);
    //set the location of the players selected texture pack
    int resID = getResources().getIdentifier(textureUsed, "drawable", "com.unreturnablestudios.FlipSide");
    //loads this texture pack
    Bitmap graphics = BitmapFactory.decodeResource(getResources(), resID);

    //src and dst are used to detarmine which parts of the image are
    //going to be used and where abouts on the screen they are going
    //to be displayed
    Rect src = new Rect(0, 0, 640, 480);
    Rect dst = new Rect(0, 0, screenWidth, screenHeight);
    canvas.drawBitmap(graphics, src, dst, null);

    // sets the style of the text up
    String scoreString = Integer.toString(score);
    Paint paint = new Paint();
    paint.setColor(Color.RED);//red font
    paint.setTypeface(Typeface.DEFAULT);//Uses the players default font
    paint.setTextSize(screenWidth / 10);//font size
    //draws the text
    canvas.drawText(scoreString,0, 0, paint);
    score -= speed;
}

// deals with user touching the screen
@Override
public boolean onTouchEvent(MotionEvent event) {
    double X = event.getX();

    long currentTime = System.currentTimeMillis();

    if (currentTime > lastTime + 100) {
        if (X < (screenWidth / 2) && X != 0) {

        } else {

        }
    }
    lastTime = System.currentTimeMillis();
    return super.onTouchEvent(event);
}

public void launchEnd() {
    Context context = getContext();
    Intent openEnd = new Intent("com.unreturnablestudios.FlipSide.END");
    context.startActivity(openEnd);
    gameLoopThread.setRunning(false);
}

public static Integer getScore() {
    // called by the End class and returns the score.
    return score;
}

public static boolean getMute() {
    // called by the End class and returns the boolean that is in control of
    // sound
    return mute;
}

// gets the measurements of the screen (width and height)
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    screenWidth = MeasureSpec.getSize(widthMeasureSpec);
    screenHeight = MeasureSpec.getSize(heightMeasureSpec);
}
}

奇怪的是我之前在此应用程序的先前版本中使用过onMeasure()并且它工作正常,但我决定改变大部分代码以尝试使其运行更快,现在它将无法工作。

1 个答案:

答案 0 :(得分:0)

OnMeasure经常被布局过程多次调用,你检查过它是不是被多次调用了吗?

通常我所看到的是,对于某些布局,它被称为第一次测量视图的宽度,然后一旦所有视图宽度都被锁定,它被称为第二次测量高度。