1

Starting to learn Canvas and have two classes so far (main one to call the view and the view) The View class onDraw creates a target (ie number of cicles and each one coloured differently)

I have a ontouch listenerer set up to record the x and y where the user clicks

my trouble then is drawing a new circle / point where the user touches

UPDATED with classes

Main Class

public class StartScreen extends Activity {

    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
}

Draw Class

public class DrawView extends View implements View.OnTouchListener {

    private Paint paint[];

    private Context context;
    private Canvas canvas;

    //definging some variables

    public DrawView(Context pContext) {
        super(pContext);  
        this.context = pContext;

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        paint = new Paint[5];
        setupColours();

    // setting varibale like raduis etc



    }

    private void setupColours() {

        // Creating Arrray of Paint Colours

    }

    @Override
    public void onDraw(Canvas pCanvas) {

        canvas = pCanvas;

        newRadius = radius;

        for (int i = 0; i < rings; i++) {


            if (i == 3) {
                canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[0]);
            } else {
                canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[1]);
            }


            canvas.drawCircle(centreWidth, centreHeight, newRadius - targetBoundary, paint[i / 2]);

            newRadius = newRadius - ringOffset;

        }

        this.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("TAG2", "x: " + event.getX() + " y: " + event.getY());
        drawHit(event.getX(), event.getY());
        return true;
    }

    public void drawHit(float hitX, float hitY) {
        Log.d("HIT", "Hit being drawn");

        Paint paint2 = new Paint();
        paint2.setColor(Color.BLACK);

        canvas.drawCircle(hitX, hitY, 100, paint2);

    }

The method is called but the circle is not being drawn. what am I doing wrong. Thanks

James Dudley
  • 915
  • 4
  • 15
  • 35
  • possible duplicate of [Draw Circle on touch](http://stackoverflow.com/questions/11796357/draw-circle-on-touch) – bummi Feb 01 '15 at 15:51

1 Answers1

3

You have provided very little information in your question. A little elaboration wouldn't have hurt. Are the targets(the circles) being created as you would want them to be? If yes, then get a hold of the FrameLayout and use the 'addView' method to overlay your ball onto the main view.

A slightly old but nevertheless a tutorial which may be useful to you: http://www.kellbot.com/2009/06/android-hello-circle/

Good Luck

Raul-
  • 147
  • 1
  • 11
  • It appears to me that you are trying to render concentric circles on the canvas. Are you facing problems rendering a single circle or is the problem arising when you extend it to the multiple circles? – Raul- Jul 04 '12 at 14:31
  • The circles work fine, its when I try and add a small circle where the user touches that does not work – James Dudley Jul 04 '12 at 14:39
  • Tried the example, its seems to work somewhat, extended to have 2 views, one for target face and another for hit. Trouble I see is the Log message for hit is being recorded over and over again. does not stop spamming the window with mesasages. why is this – James Dudley Jul 04 '12 at 14:41
  • Is 'Tag2:' getting logged several times too? Btw, try making 'hitx' and 'hity' static variables in 'DrawView' and inside 'onTouch' modify them to the latest touch coordinates. Also insert: canvas.drawCircle(hitX, hitY, 100, paint2); inside 'onDraw' too. – Raul- Jul 04 '12 at 14:58