Wow.. Thank you .. Very detailed and helpful answer.. Helped me a lot.. The
problem was the calculation of radius from the points.. Thanks again :)


On Fri, Feb 22, 2013 at 8:42 PM, Nobu Games <dev.nobu.ga...@gmail.com>wrote:

> Alright, let's try to help you again. First of all your problem is as
> follows:
>
> 1. On first touch (ACTION_DOWN) you determine the center point "C" of the
> circle.
> 2. While moving the finger you determine the radius of the circle by
> measuring the distance from the center point C to the current move
> coordinate "M".
> 3. On ACTION_UP the drawn circle gets "permanently" drawn to the drawing
> surface
>
> You already solved number 1. Center point C gets stored in downx and downy.
>
> Number two isn't that complicated either. First of all get rid of your
> "axis" Collection objects. You can calculate the circle's radius right
> there by measuring the distance from (downx;downy) to your current touch
> event point. By the way you can use the existing Android framework method 
> PointF.length(float,
> float)<http://developer.android.com/reference/android/graphics/PointF.html#length%28float,%20float%29>for
>  that.
>
> Now to the tricky part and I believe that is where your actual problem
> stems from. The difficulty is having both a "live" update of the circle
> while resizing it and displaying all previously drawn circles at the same
> time.
>
> I assume you created your own custom view with an overridden
> onDraw(Canvas)<http://developer.android.com/reference/android/view/View.html#onDraw%28android.graphics.Canvas%29>method.
>  That onDraw method is meant for drawing the current state of the
> view. So on each update you start with a completely blank canvas and you
> have to re-create all previously drawn circles and the one you are
> currently drawing using touch input.
>
> In order to get that right you have to do the following:
>
>    - let your onTouch event handler method invalidate the drawing view.
>    - let your drawing view's onDraw method draw all previously rendered
>    circles
>    - let your drawing view's onDraw method render the currently drawn
>    circle as follows:
>
> float radius = Math.sqrt(Math.pow(downx - movex, 2) + Math.pow(downy -
>> movey, 2));
>> canvas.drawCircle(downx, downy, radius, paint);
>>
>
>
> For re-creating the previously drawn circles you have two options:
>
> In your ACTION_UP case block you can...
>
>    1. ...store all circle center points and their radiuses in a list data
>    structure. In onDraw you need to iterate that list and re-draw each circle
>    2. ...render that circle in a separate Bitmap object that contains all
>    already drawn circles.
>
> Option number 2 is much more efficient because if you would implement
> number 1: the more you draw the more work for your view's onDraw method on
> each rendering update.
>
> You can implement option number 2 by creating a Bitmap of the size of your
> drawing view. Then you need to create a Canvas object for that bitmap:
>
> drawnCirclesBitmap = Bitmap.createBitmap(drawingView.getWidth(),
>> drawingView.getHeight(), Bitmap.Config.ARGB_8888);
>> drawnCirclesCanvas = new Canvas(drawnCirclesBitmap);
>
>
> use the drawnCirclesCanvas for drawing your finished circles in the
> ACTION_UP case block. Then you need to change your drawing view's onDraw
> method a bit. It has to draw the bitmap "drawnCirclesBitmap" at the
> position (0;0):
>
> protected void onDraw(Canvas canvas) {
>>     canvas.drawBitmap(drawnCirclesBitmap, 0, 0, null);
>>     canvas.drawCircle(downx, downy, radius, paint);
>> }
>>
>
>
> On Friday, February 22, 2013 3:20:59 AM UTC-6, Numair Qadir wrote:
>>
>> Hi! Greetings!
>> I'm working in onTouch method to draw a circle, what i'm trying is to
>> draw a circle when user draws on the screen, without using any fixed
>> radius. Radius should calculate from the distance. I've tried but it is not
>> drawing what i want. Here is my code
>>
>> @Override
>> public boolean onTouch(View bg, MotionEvent event) {
>> // TODO Auto-generated method stub
>> dumpEvent(event);
>>
>> final int action = event.getAction();
>> switch (action & MotionEventCompat.ACTION_MASK) {
>> case MotionEvent.ACTION_DOWN:
>> downx = event.getX();
>> downy = event.getY();
>> xaxis.clear();
>> yaxis.clear();
>> Log.d(TAG, "DOWN");
>> break;
>>
>> case MotionEvent.ACTION_MOVE:
>> //drawCircle(event);
>> for (int i = 0; i < event.getPointerCount(); i++) {
>> xaxis.add(Integer.valueOf((**int) event.getX(i)));
>> yaxis.add(Integer.valueOf((**int) event.getY(i)));
>> }
>> // Collections.sort(xaxis);
>> // Collections.sort(yaxis);
>>  Log.d(TAG, "DRAG");
>> return true;
>>
>> case MotionEvent.ACTION_UP:
>> upx = event.getX();
>> upy = event.getY();
>>
>> if (xaxis.size()==0 && yaxis.size()==0){
>> Log.d(TAG, "Arraylist is empty");
>> }
>> else {
>> minx = xaxis.get(0);
>> miny = yaxis.get(0);
>>  for (int i = 0; i < xaxis.size(); i++){
>> maxx = xaxis.get(i);
>> }
>> for (int j = 0; j < yaxis.size(); j++) {
>> maxy = yaxis.get(j);
>> }
>>  double temp = Math.pow((maxx - minx), 2)
>>  + Math.pow((maxy - miny), 2);
>>  float radius = (float) Math.sqrt(temp) / 2;
>> canvas.drawCircle(minx, miny, radius, paint);
>>  }
>>  choosenImageView.invalidate();
>> mode = NONE;
>> Log.d(TAG, "mode=NONE");
>> return true;
>>
>>  }
>> choosenImageView.invalidate();
>> return true;
>> }
>>
>>
>> Help needed!
>>
>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-developers+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to