Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android onMeasure()测量宽度,但不测量高度_Android_View_Size_Measure - Fatal编程技术网

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

Android onMeasure()测量宽度,但不测量高度,android,view,size,measure,Android,View,Size,Measure,我有一个SurfaceView,我需要知道的宽度和高度,代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); screenWidth = MeasureSpec.getSize(widthMeasureSpec); screenHeight = MeasureSpe

我有一个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);
}
}
公共类GameView扩展了SurfaceView{
GameLoopThread GameLoopThread;
整数屏幕宽度,屏幕高度;
静态整数分;
长时间=0;
字节速度=5;
静态布尔静音;
静态字符串textureUsed=“space”;
公共游戏视图(上下文){
超级(上下文);
静音=主菜单。getMute();
得分=0;
gameLoopThread=新gameLoopThread(此);
SurfaceHolder holder=getHolder();
holder.addCallback(新回调(){
公共无效表面更改(表面文件夹持有者,整数格式,
整数宽度,整数高度){
}
已创建的公共空白表面(表面持有人){
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
公共空间表面覆盖(表面覆盖物持有人){
布尔重试=真;
gameLoopThread.setRunning(false);
while(重试){
试一试{
gameLoopThread.join();
重试=错误;
}捕捉(中断异常e){
}
}
}
});
}
//这里画的是图形
@凌驾
受保护的void onDraw(画布){
//用白色覆盖整个画布以清除先前绘制的图形
画布。drawColor(颜色。白色);
//设置玩家所选纹理包的位置
int resID=getResources().getIdentifier(使用纹理,“drawable”,“com.unreturnablestudios.FlipSide”);
//加载此纹理包
位图图形=BitmapFactory.decodeResource(getResources(),resID);
//src和dst用于确定图像的哪些部分是
//将要被使用,以及他们将在屏幕上的位置
//展示
Rect src=新的Rect(0,060480);
Rect dst=新的Rect(0,0,屏幕宽度,屏幕高度);
drawBitmap(图形、src、dst、null);
//设置文本的样式
字符串scoreString=Integer.toString(分数);
油漆=新油漆();
paint.setColor(Color.RED);//红色字体
paint.setTypeface(Typeface.DEFAULT);//使用默认字体
paint.setTextSize(屏幕宽度/10);//字体大小
//绘制文本
drawText(scoreString,0,0,paint);
分数-=速度;
}
//处理用户触摸屏幕的问题
@凌驾
公共布尔onTouchEvent(运动事件){
double X=event.getX();
长currentTime=System.currentTimeMillis();
如果(当前时间>上次时间+100){
如果(X<(屏幕宽度/2)和&X!=0){
}否则{
}
}
lastTime=System.currentTimeMillis();
返回super.onTouchEvent(事件);
}
公共无效启动(){
Context=getContext();
Intent openEnd=newintent(“com.unreturnablestudios.FlipSide.END”);
背景。起始触觉(开放性);
gameLoopThread.setRunning(false);
}
公共静态整数getScore(){
//由结束类调用并返回分数。
返回分数;
}
公共静态布尔getMute(){
//由End类调用,并返回控制
//声音
返回静音;
}
//获取屏幕的测量值(宽度和高度)
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
screenWidth=MeasureSpec.getSize(widthMeasureSpec);
屏幕高度=测量等级getSize(高度测量等级);
}
}

奇怪的是,我以前在这个应用程序的前一个版本中使用过onMeasure(),它工作得很好,但我决定修改大部分代码,使它运行得更快,现在它不工作了。

onMeasure经常被布局过程调用多次,您检查过它没有被多次调用吗


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

如何实例化它?在代码或XML中,请根据需要显示。我只是再次查看了代码,似乎度量值不是问题,因为某种原因,它是src矩形,我试图绘制一个480px高的图像的一部分,但如果我使用480的值,它会被截断,然后我需要使它看起来像图像没有正确缩放。