开发者

How do you make something work after two clicks

I am trying to mak开发者_JAVA百科e an application for android and I think I need to make the program get one (X,Y) value for one point and then another after another click so that way I can draw a line between the two points that the user clicks. If there is a better way to get X,Y information, then I would like to know. Thank you.

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 x = event.getX();
        double y = event.getY();
        return true;
    }
}); 


I'd do something like this...

public class MyActivity extends Activity
    implements View.OnTouchListener {

    int touchCount = 0;
    double startX, startY, endX, endY;
    View touchView = null;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set content view on the next line to whatever your
        // layout XML file is. For example main.xml
        setContentView(R.layout.main);
        touchView = findViewById(R.id.touchView);
        touchView.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.equals(touchView)) {
            if (touchCount == 0) {
                startX = event.getX();
                startY = event.getY();
                touchCount++;
            }
            else {
                endX = event.getX();
                endY = event.getY();
                touchCount = 0;
                DrawLine();
            }
            return true;
        }
        return false;
    }

    private void DrawLine() {
        // Process the start / end X,Y points to draw a line here
    }
}


Your function for getting x and y looks alright. What I would do is just put in the x and y coordinates in a global x1 and y1 variable and then the second time I click/touch put them in x2 and y2 and then you draw a line when you have values for your global vars...

You could do that with an if statement or something of the sort. If you need more than two points you could maybe put all your x and y coordinates in an array and just update the view to paint a line between each new point added to the array...

EDIT

Here's just a snipped but you'd need a cleaner version of this:

double x1 = null;
double y1 = null;
double x2 = null;
double y2 = null;

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()));
        if (x1 == null) {
            x1 = event.getX();
            y1 = event.getY();
        } else {
            x2 = event.getX();
            y2 = event.getY();
        }

        // draw a line between x1,y1 and x2,y2 here...

        return true;
    }
});

To mention this again... This is really bad code what I wrote up there, I just wrote it to show what I meant in my answer. Once you know exactly what you want and how many points and lines to draw you can modify and use arrays or anything else you might need in your implementation.

Hope this helps and clears up my answer?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜