嗨,我为我的申请画了一幅画布
这是代码我的画布

public class Canvas1 extends View{
int i;
int k;
float l,m;
    public Canvas1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public Canvas1 (Context context, AttributeSet attrs) {
        super(context, attrs);

        }
    public Canvas1 (Context context, AttributeSet ats, int ds) {
        super(context, ats, ds);

        }


    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawRGB(255, 255, 255);
        Paint paint1=new Paint(Color.BLACK);
        Paint paint=new Paint(200);
        paint.setStrokeWidth((float)0.5);
        canvas.drawLine(0,0,0,getMeasuredHeight(), paint1);
        canvas.drawLine(0,0, getMeasuredWidth(), 0, paint1);
        canvas.drawLine(0,getMeasuredHeight(), getMeasuredWidth(),getMeasuredHeight(),  paint1);
        canvas.drawLine(getMeasuredWidth(),getMeasuredHeight(), getMeasuredWidth(), 0, paint1);

    //canvas.drawLine(0, 0, ((float)getMeasuredWidth()), ((float)getMeasuredHeight()), paint);
    if(l!=0||m!=0)
    {
        canvas.drawLine(getMeasuredWidth()/2,getMeasuredHeight()/2,getMeasuredWidth()/2,getMeasuredHeight(),paint);
        canvas.drawLine(0,0,getMeasuredWidth()/2,getMeasuredHeight()/2,paint);


    }

        for(int i=1;i<5;i++)
    canvas.drawLine(((float)getMeasuredWidth())/5*i, 0, ((float)getMeasuredWidth()/5*i),getMeasuredHeight(), paint);
        for(int i=0;i<5;i++)
            canvas.drawLine(0, getMeasuredHeight()/5*i, getMeasuredWidth(), getMeasuredHeight()/5*i, paint);
           canvas.save();
    }

public Boolean setline(float d,float e){


    l=d;
    m=e;
    Log.i("Hello Canvas",l+"  "+m+"  ");
    return true;

}



}

现在我试图从我的主活动中调用函数setline(),在这个活动中有一个画布数组。这是我活动的代码
public class HelloCanvasActivity extends Activity {
    /** Called when the activity is first created. */
    Integer[] in={R.id.can1,R.id.can2,R.id.can3,R.id.can4,R.id.can5,R.id.can6,R.id.can7,R.id.can8};
    float x,y;
    int j=0;
    Canvas1[] can=new Canvas1[8];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        for(int i=0;i<8;i++)
        {
            can[i]=(Canvas1)findViewById(in[i]);

        }
        can[0].setline((float)0.5,(float) 0.5);
        new Thread(new Runnable(){
            public void run(){



            for(int i=0;i<8;i++){

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            can[i].setline((float)0.5,(float)0.5);


            }


}



               }).start();




            }


    }

当我忽略线程时,代码行中的sleep(1000)正在绘制,但是当我放置线程时,sleep(1000)setline在1秒后调用,我正在hello world的日志表中获取日志,但是没有行在画布上绘制,请帮助我找到错误的地方。

最佳答案

setline方法中,调用invalidate()

10-08 17:13