2013-06-25 54 views
0

为什么当我在画布对象 上使用drawRect()并且在onCreate方法中声明它时,矩形不显示。为什么当我使用drawRect()时矩形不显示?

代码

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activitymain); 
    Chronometer stopWatch = (Chronometer)findViewById(R.id.chrono); 
    mDrawingActivity = (DrawingActivity)findViewById(R.id.the_canvas); 
    Button b = (Button)findViewById(R.id.startButton); 
    b.setText("start"); 
    b.setOnClickListener(this); 
} 

的OnDraw()方法

protected void onDraw(Canvas Square) 
    { 
     super.onDraw(Square); 
      Paint squareColor = new Paint(); 
      squareColor.setColor(Color.CYAN); // change the box color to cyan 
     Square.drawRect(100,100,100,100, squareColor); 
return; 
    } 

澄清:即使是按钮和记时计没有示出过和程序被强制关闭。

+5

请提供您正在绘制矩形的代码 – Dimmerg

+0

只需在文档中阅读Canvas.drawRect()的参数'right'和'bottom'就意味着什么 –

回答

7

您正在绘制一个点矩形。 更改行

Square.drawRect(100,100,100,100, squareColor); 

Square.drawRect(100, 100, 200, 200, squareColor) 

下面是从文档的定义。

drawRect(float left, float top, float right, float bottom, Paint paint) 

使用指定的绘图绘制指定的Rect。该矩形将根据油漆中的样式进行填充或框定。

参数 左矩形的左侧被吸入 顶部要绘制的矩形的顶侧 右矩形的右侧要绘制 底部的矩形的底侧被吸入 油漆用于绘制矩形的油漆

相关问题