Java 如何在Android中缩放画布
我正在编写一个android游戏,使用Canvas作为绘制一切的方式,问题是当我在不同的android手机上运行它时,画布的大小没有改变,我尝试使用Canvas.scale(),但这并没有产生任何影响。我用于绘图的代码是Java 如何在Android中缩放画布,java,android,android-canvas,Java,Android,Android Canvas,我正在编写一个android游戏,使用Canvas作为绘制一切的方式,问题是当我在不同的android手机上运行它时,画布的大小没有改变,我尝试使用Canvas.scale(),但这并没有产生任何影响。我用于绘图的代码是 public void draw( Canvas c, int score ) { Obstical2[] obstmp = Queue.toArray(this.o); Coin[] cointmp = QueueC.toArray(this.c);
public void draw( Canvas c, int score )
{
Obstical2[] obstmp = Queue.toArray(this.o);
Coin[] cointmp = QueueC.toArray(this.c);
for( int i = 0; i < obstmp.length; i++ )
{
obstmp[i].draw(c);
}
for( int i = 0; i < cointmp.length; i++ )
{
cointmp[i].draw(c);
}
c.drawText(String.format("%d", score ), 20, 50, textPaint);
if( isWon && isStarted ) c.drawText("YOU WON", 20, 400, resPaint);
else if( isLost && isStarted ) c.drawText("YOU LOST", 20, 400, resPaint);
}
硬币的绘图功能:
public void draw( Canvas c )
{
obstical.draw(c);
}
如何使画布重新调整大小,使其在任何屏幕上看起来都一样
Cheers Daniel重写onMeasure()函数,然后计算屏幕宽度和屏幕高度,然后计算比例
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
screen_width = MeasureSpec.getSize(widthMeasureSpec);
screen_height= MeasureSpec.getSize(heightMeasureSpec);
}
试试这个,这会改变你的屏幕大小
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
screen_width = w;
screen_height= h;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
screen_width = w;
screen_height= h;
}