Reputation: 957
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
Upvotes: 1
Views: 6426
Reputation: 157
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
Upvotes: 3