2

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.

malaka
  • 85
  • 9

1 Answers1

2

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;
TomTsagk
  • 1,474
  • 9
  • 23
  • I am using canvas. I was reading and now I can draw the circle but I want to make it with a dynamic radius. I read taht [link](http://stackoverflow.com/questions/8974088/how-to-create-a-resizable-rectangle-with-user-touch-events-on-android/17807469#17807469) but I cannot draw the rectangle without the balls. Any idea? – malaka May 27 '15 at 14:48
  • @AbelSuviri I edited my answer with more explanation – TomTsagk May 27 '15 at 15:03
  • I test your code and I change it a bit to fit my code but it does not work properly. I get a big circle allways at the left side of the screen and I am unable to move it to the position and when I stop touching the screen I get a small circle in the position I touched and the big circle dissapears – malaka May 28 '15 at 08:14
  • @AbelSuviri the big circle must mean that you are drawing the shape while not touching the screen, in my code i set `x = -1` when user lifts finger from screen and then on the draw function i test `if (x != -1)` to check if user is touching the screen (you only want to draw circle then) have u done that correctly ? – TomTsagk May 28 '15 at 13:15
  • I used your code without change anything, only checking if I choose to draw a circle and it works. The problem is I cannot keep the circle drawing on the canvas. I tried adding invalidate() but I cannot keep it drawing. – malaka May 28 '15 at 14:15
  • @AbelSuviri you should create a new `Bitmap` on the constructor and connect it with a new `Canvas`. Where I say `//Draw circle inside your bitmap here` on the `ACTION_UP` you should do something like `myCanvas.drawCircle(radius, x, y, paint);` and on the `draw` function you should draw your `Bitmap` that is connected with that canvas. – TomTsagk May 28 '15 at 14:25
  • Ok, I found the problem. I did not add the ´drawcircle´ into the ´ACTION_UP´. Tomorrow I will try to draw a rectangle and if I need help I will be back for ask you. Thank you so much. – malaka May 28 '15 at 14:38
  • @AbelSuviri ok! feel free to ask me if anything goes wrong! – TomTsagk May 28 '15 at 14:43