开发者

android how to draw circles ,rectangles on canvas?

开发者 https://www.devze.com 2023-03-25 17:43 出处:网络
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

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 cli开发者_如何学运维ck? Give me a way. Thank you


//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;
    }
}


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.

0

精彩评论

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