canvas.drawline() 不起作用

发布于 2024-12-11 07:06:01 字数 3085 浏览 0 评论 0原文

您好,我为我的应用程序绘制了画布 这是我的画布的代码

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();




            }


    }

,当我从正在绘制的代码行中省略 Thread.sleep(1000) 时,但是当我将 Thread.sleep(1000) setline 在 1 秒后调用时,我将登录到 Hello World 日志表中,但画布上没有画线,请帮助我找出错误的地方。

Hi i draw a canvas for my application
here is the code my canvas

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;

}



}

now i am trying to call the function setline() from my main activity in this activity there is an array of canvas . here is the code of my activity

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();




            }


    }

when i omit Thread.sleep(1000) from my code lines are drawing but when i put Thread.sleep(1000) setline is calling after 1 second i am getting log in my log table of Hello World,but no line is drawing on canvas please help me in finding where i am wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒路情人 2024-12-18 07:06:01

在您的 setline 方法中,调用 invalidate()

In your setline method, call invalidate()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文