nickfrancis.me
nickfrancis.me

Reputation: 271

android how to draw circles ,rectangles on canvas?

In my app i am able to draw something on canvas within on touch event.

But problem is, at a time single item can be draw within touch event. means if i will put lineto() and moveto() then it ll draw lines. if addCircle() ll given then circle drawn.and also for Rect and oval.

But i want to give different buttons for that. By default line will be drawn. If i press Circle then circle will drawn, if Rect button press then Rectangle will draw on canvas by using same touch event.

So i want to know what i ll write under on touch events so that it will work for every button click? Give me a way. Thank you

Upvotes: 2

Views: 2664

Answers (2)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

//THESE ARE GLOBAL!
boolean isDrawing = false;
boolean circle = true; //default
boolean rect = false;
boolean line = false;

Create three buttons:

Button circleB,rectB,lineB;

In circleB (onClick):

boolean circle = true; //set circle to true
boolean rect = false;
boolean line = false;

In rectB (onClick):

boolean circle = false;
boolean rect = true; //set rect to true
boolean line = false;

In lineB (onClick):

boolean circle = false; 
boolean rect = false;
boolean line = true; //set line to true

For circle size:

//Global
int size = 10; //Default:
boolean isScaling=false;

public boolean onTouch(View arg0, MotionEvent e) {
switch(e.getAction()){
    case(MotionEvent.ACTION_DOWN):
        isScaling=true;
    break;
    case(MotionEvent.ACTION_MOVE):
        if(isScaling){
            size++; //increment as you want
        }
    break;
    case(MotionEvent.ACTION_UP):
        isScaling=false;
        if(!isDrawing){
            isDrawing=true;
            if(circle)
            {
                //code to Draw Circle
            }
            else if(rect)
            {
                //code to Draw Rect
            }
            else if(line)
            {
                //code to Draw line
            }
            isDrawing=false;
        }
        //reset size I think it is better to reset it
    break;
    }
}

Upvotes: 2

Maxim
Maxim

Reputation: 3006

I'm not sure that I correctly understand what you are talking about. But if I do it could be done with canvas.drawCircle, canvas.drawRect methods.

Upvotes: 0

Related Questions