0

shapeview 是一个自定义视图。我想要的是在单击按钮时调用 ondraw 方法。看法:

 public class ShapeView extends View {

Paint cPaint;
Random r=new Random();
public static int n=3;
public ShapeView(Context context,AttributeSet attrs) {
    super(context,attrs);
    // TODO Auto-generated constructor stub
    cPaint = new Paint();
    cPaint.setColor(Color.BLACK);


}
public ShapeView(Context context){
    super(context);
}
public void onDraw(Canvas canvas)
{
    Log.e("LineView", Integer.toString(n));
    int n1=1;
    while(n1<=n)
    {
        Log.d("draw","n= " +n);
        int x=r.nextInt(canvas.getWidth()-50);
        int y=r.nextInt(canvas.getHeight()-200);
        canvas.drawCircle(x, y, 10, cPaint);
        n1++;
    }
}
public void nextshape(int n)
{
    this.n=n;
    Log.d("n","n= " +n);
    invalidate();

}

}

nextshape 方法在单击添加此自定义视图的活动中的按钮时调用。这里是它的代码。

 public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.next_button:
        i=i+1;
        Log.d("shape","i= "+i);
        shape.nextshape(i);
        break;

    }

我正在使用 xml 文件将视图添加到活动中。onDraw 方法只运行了 2 或 3 次。之后什么也没有发生。这段代码有什么问题?

4

0 回答 0