我有一个我需要知道的宽度和高度,代码看起来像这样:
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(),并且效果很好,但是我决定更改大多数代码以使其运行速度更快,现在它无法正常工作。
通常通过布局过程多次调用Measure,您是否检查了它的调用不超过一次?
我通常看到的是,在某种布局中,这被称为测量视图宽度的第一次,然后一旦所有视图宽度都被锁定,它被称为第二次来测量高度。