0

I want to place red circles where I touch an image and have a class that listens to the touch and sends the coordinates to an other class:

public class Report extends Fragment {
private Context activity;
private Point point = new Point();
private DrawingCrl imgCircle = new DrawingCrl();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.report, container,
            false);
    activity = this.getActivity();

            //Where I'm doing the touching and respond to that:
    final View touchView = rootView.findViewById(R.id.ImageC);
    touchView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            point.x = Float.valueOf(event.getX());
            point.y = Float.valueOf(event.getY());
                            touchView = imgCircle.DrawCircle(Point point);
            return true;
        }
    });

    return rootView;

    public class Point {
        float x;
        float y;
}

}

When I call the DrawCircle I want the class DrawingCrl to do just that, draw a circle for me on the point I send to the method:

public class DrawingCrl {
private Bitmap mBitmap;
private Paint paint;
    private Canvas canvas = new Canvas();

public void DrawCircle(Point point) {
    mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
    paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL);
    canvas.drawCircle(point.x, point.y, 50, paint);
}
}

I have read both getting X and Y coordinates and drawing circle and Draw Circle on touch but don't get how to do the drawing of the circle.

I'm pretty new to android so sorry my many noob failures. I hope you can help me get this working! Thanks! :)

Community
  • 1
  • 1

1 Answers1

0

Okay, here's how I would do it but I'm going to change your code a bit.

First I would change your DrawingCrl class to just store location (and possibly Paint object but this would probably fit better as a static shared by all DrawingCrls) like this:

public class DrawingCrl {
    public Point myLoc;
    public Paint paint;

    public void DrawCircle(Point point) {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        myLoc = point;
    }
}

Save a Vector of DrawingCrl objects created in your Activity and create new ones in the onTouch event handler.

This goes in the Report class:

java.util.Vector<DrawingCrl> myCircles = new java.util.Vector<DrawingCrl>();

Then in 'onTouch' create a new circle object and save it:

  @Override
  public boolean onTouch(View v, MotionEvent event) {
        point.x = Float.valueOf(event.getX());
        point.y = Float.valueOf(event.getY());

        // create a new Circle here and save it
        DrawingCrl c = new DrawingCrl(point);
        myCircles.add(c);
        return true;
    }

Then I would override the 'onDraw' method of your View 'touchView'.

The 'onDraw' will give you the Canvas to draw to, so don't create your own like you are doing in your DrawingDMG class right now.

@Override
protected void onDraw(Canvas c)
{
  for (DrawingCrl c : myCircles)
  {
    c.drawCircle(c.myLoc.x, c.myLoc.y, 50, c.paint);
  }
}
spartygw
  • 3,289
  • 2
  • 27
  • 51
  • Like this: '@Override protected void onDraw() { DrawCircle(); } ' But how to make the onDraw methode to run? And is this all? Do you got a small example? – user2373869 May 13 '13 at 17:18
  • The onDraw will get called by the View when it needs to draw itself. You don't call it directly. By the way, that code was just typed in the stackoverflow editor. There may be a typo but it should get you real close to what you want. – spartygw May 13 '13 at 19:54