我有一個SurfaceView,我需要知道的寬度和高度,代碼如下:的Android onMeasure()測量的寬度,但高度不
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()之前,它的工作很好,但我決定改變大多數代碼嘗試使它運行得更快,現在它不能工作。
你如何實例化它?在代碼或XML中,請適當顯示。 – Simon