malaka
malaka

Reputation: 95

Draw shapes dynamic

I am developing an application for technical drawing and I need to add a few tools to draw lines, circles, rectangles and corners. Now I can draw lines and free hand drawing but I cannot draw circles, rectangles and corners. I found in lot of websites how to draw it but static, I mean, draw the shape in the position you pre-set or the position where you touch but I need to know how to draw, for example, a circle in the position I touch and make it bigger than I separate my fingers. I hope you understand what I mean.

Upvotes: 2

Views: 1599

Answers (1)

Tom Tsagkatos
Tom Tsagkatos

Reputation: 1494

You can have two variables x and y, then every time you touch the screen set x and y to that value, while drawing draw the circle with coordinates x and y.

If you are drawing and you just want to keep a painted circle, you can paint the circle and add it inside your canvas on x and y, then the next time you touch the screen a new circle will be painted on x and y and the old one will remain painted.

Are you using Canvas ? if so you can find out how to do this here (Canvas documentation) and here (Bitmap documentation). Depending on your situation you can create a new Bitmap and assign it to Canvas then draw on the Canvasand inside your bitmap you will have your desired circles and other shapes, on the next drawing frame, draw new shapes and the changes will remain.

Edit: In order to have dynamic radius follow this logic, when you touch the screen, set x and y to that point (the center of the circle), while moving the finger on the screen, calculate the radius comparing to x and y, when lifting your finger apply the drawing on the bitmap as told above.

Some code:

public boolean onTouchEvent(MotionEvent e)
{
    switch (e.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            x = (int) event.getX();
            y = (int) event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            //If I'm not wrong this is how to calculate radius,
            //I'm at work and can't test this now, you can use your way
            int distanceX = (int) event.getX() -x;
            int distanceY = (int) event.getY() -y;
            radius = sqrt(distanceX *distanceX + distanceY *distanceY);
            break;
        case MotionEvent.ACTION_UP:
            //Draw circle inside your bitmap here

            //This is like a flag to notify the program that no new shape is being drawn
            x = -1;
            break;
}

public void draw(Canvas canvas)
{
    canvas.drawBitmap(myBitmap, 0, 0, null);

    //A new shape is being drawn
    if (x != -1)
        //In here you place a switch to decide which shape you are drawing
        //for this example i assume circle
        canvas.drawCircle(radius, x, y, paint);
}

When you are lifting your finger the new circle should be painted on your bitmap so you don't have to add extra code for each new circle.

Edit2: I will add more code with the Bitmap and Canvas method i described.

Bitmap myBitmap;
Canvas myCanvas;

//Constructor
public myActivity(Bundle bundle) //or whatever your constructor is
{
    myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    myCanvas = new Canvas(myBitmap);
}

Now anything you draw on "myCanvas" will be applied to "myBitmap", when ACTION_UP activates draw circle on "myCanvas" which is drawn on the draw function.

case MotionEvent.ACTION_UP:
    myCanvas.drawCircle(radius, x, y, paint);

    x = -1;
    break;

Upvotes: 2

Related Questions