简体   繁体   English

为什么在通过canvas.getWidth()方法进行x坐标处理后,仍未绘制该对象?

[英]Why after passing canvas.getWidth() method to x coordinate the object is not being drawn?

I created a class named MyButton, to help me draw rect and doing some actions when its beeing pressed, but when Im passing to the x and y coordinates values, using math on the canvas.getWidth() and canvas.getHeight() method, the button is not being drawn: 我创建了一个名为MyButton的类,以帮助我在按下按钮时绘制矩形并执行一些操作,但是当Im通过canvas.getWidth()和canvas.getHeight()方法的数学运算传递给x和y坐标值时,该按钮未绘制:

This is the methoed to set the coordinates and the height and width of the button: 这是设置按钮的坐标以及高度和宽度的方法:

public void setVars(float x, float y, float width, float height)
    {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

this is what I tried and didnt work: 这是我尝试过但没有起作用的:

this.buttonHint.setVars((float)0, (float)(canvas.getHeight() - canvas.getHeight()/20), (float)(canvas.getWidth()/2), (float)(canvas.getHeight()/20));
this.buttonSuperHint.setVars((float)(canvas.getWidth()/2), (float)(canvas.getHeight() - canvas.getHeight()/20), (float)(canvas.getWidth()/2), (float)(canvas.getHeight()/20));

but when I try this, it works: 但是当我尝试这样做时,它的工作原理是:

    this.buttonHint.setVars((float)0, (float)(20), (float)(canvas.getWidth()/2), (float)(canvas.getHeight()/20));
    this.buttonSuperHint.setVars((float)(20), (float)(20), (float)(canvas.getWidth()/2), (float)(canvas.getHeight()/20));

probably the width/height of the canvas you are calling is still 0 at that time. 那时您正在调用的画布的宽度/高度可能仍为0。 add some listeners of the width/height of the canvas and make sure it has it's height/width set other than 0, then try calling that button attributes. 添加一些画布的宽度/高度的侦听器,并确保其高度/宽度设置为非0,然后尝试调用该按钮属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM