简体   繁体   中英

Simple Drawing App In Android

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

You can try this

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

Basically, this should align the canvas below the button. Currently it is overlapped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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