简体   繁体   English

如何在 java 的 IF 语句中画一条线

[英]How to draw a line in an IF statement for java

I am trying to program an androd application where if there is input on two places of the screen in sucsession then it will draw a line between the two points.我正在尝试编写一个 androd 应用程序,如果在屏幕的两个位置连续输入,那么它将在两点之间画一条线。 I have already set up "X" and "Y" values that work and columns and rows are defined by the "X" and "Y" values.我已经设置了有效的“X”和“Y”值,列和行由“X”和“Y”值定义。 After those i have an IF statement that needs to draw a line between the two points.在这些之后,我有一个 IF 语句需要在两点之间画一条线。 Say if column one and row two are selected and then colum one and row three are selcted I want a line to be drawn between the two points.假设如果选择了第一列和第二行,然后选择了第一列和第三行,我希望在两点之间画一条线。 Also I am not totally sure how to use the MotionEvent stuff or how to put the touch actions into the IF statement.我也不完全确定如何使用 MotionEvent 的东西或如何将触摸动作放入 IF 语句中。

     final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
    @Override         
    public boolean onTouch(View v, MotionEvent event) { 
            String.valueOf(event.getX() + String.valueOf(event.getY()));
        double c = event.getX();
        double column = Math.floor(event.getX()/(480/12));
        double r = event.getY();
        double row = Math.floor(event.getY()/(630/12));


    if (column == 0 && row == 2 //there should be more stuff here 
                   ) {
                  //I dont know how to draw a line in here, please help
    }
        return true;     
        }
    });   
}

Rather than explain the details here, I'll point you to these pieces of sample code from the ApiDemos sample project that comes with the SDK, that probably do exactly what you want:我不会在这里解释详细信息,而是向您指出 SDK 附带的ApiDemos示例项目中的这些示例代码,它们可能完全符合您的要求:

The basic idea is to store X and Y coordinates in your touch event handler, invalidate the View , and then draw the lines in the onDraw method using Canvas operations such as drawLine .基本思想是将 X 和 Y 坐标存储在您的触摸事件处理程序中,使View无效,然后在onDraw方法中使用诸如drawLineCanvas操作绘制线条。

You do neew to have a tool to draw the line, most suitable for you it seems to be Canvas.你确实需要一个画线的工具,最适合你的似乎是 Canvas。 If you don't know anything about Canvas together with Android yet i suggest you to look into some examles that Android leaves us.如果您对 Canvas 和 Android 一无所知,但我建议您查看 Android 离开我们的一些示例。 Ones you have done that, this is going to be a simple task.你已经做到了,这将是一项简单的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM