1

我有一个 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() 并且它运行良好,但我决定更改大部分代码以尝试使其运行得更快,但现在它无法运行。

4

1 回答 1

0

OnMeasure 经常被布局过程调用多次,你检查过它没有被多次调用吗?

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

于 2012-11-11T20:00:41.267 回答