1

As a beginner, I'm trying to create a simple drawing application in android. However, I keep running into the following issue where I am not able to press any of my buttons because a path is drawn on top of the button. How would I adjust the following code so that the ability to draw is separated from pressing buttons?

I am definitely also open to suggestions on how to decouple the drawing experience from the button clicking experience, but I am more interested in how to resolve the issue with the code I currently have as a learning experience.

To give you a better example of what I mean is : http://imgur.com/8i1RCt7

Simplified Code:

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

    setContentView(R.layout.activity_main);

    aButton = (Button) findViewById(....
        bButton = (Button) findViewById(....

    aListener = new OnClickListener(){...
    bListener = new OnClickListener(){...

    nextButton.setOnClickListener(aListener);
    clearButton.setOnClickListener(bListener);

    layout = (RelativeLayout) findViewById(R.id.activity_main);
    cView = new CanvasView(this);

    layout.addView(cView);

    listener = new OnTouchListener(){

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            float posX = event.getX();
            float posY = event.getY();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                     path.moveTo(posX, posY);
                     break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                     path.lineTo(posX, posY);
                     break;
            } // ends switch statement
            cView.invalidate();
            return true;

        }

    };
    cView.setOnTouchListener(listener);
4

1 回答 1

1

你可以试试这个

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
cView.addRule(RelativeLayout.BELOW , aButton.getId());
layout.addView(cView, p);

基本上,这应该对齐按钮下方的画布。目前它是重叠的。

于 2013-09-16T22:58:20.317 回答