1

I have a SurfaceView that i need to know the width and height of, the code looks like this:

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

For some reason it manages to measure the width of this view but not the height of it, I've tried placing the height measurement before the width one to see if its just not calling the onMeasure() method quick enough but it didn't work.

Edit:

Heres all the code:

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);
}
}

The weird thing is i have used onMeasure() before in a previous version of this application and it worked fine, but i decided to alter most of the code to try and make it run faster and now it won't work.

4

1 に答える 1

0

OnMeasure は、レイアウト プロセスによって複数回呼び出されることがよくあります。複数回呼び出されていないことを確認しましたか?

通常、私が見たのは、一部のレイアウトでは、ビューの幅を測定するために最初に呼び出され、すべてのビューの幅がロックされると、高さを測定するために 2 回目の呼び出しが行われるということです。

于 2012-11-11T20:00:41.267 に答える