Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on an Android game where I need to draw a 2d array of circles onto a custom view. I have the method to draw the circles, but don't know how to call it. Here is the code for it.
Java
public Canvas drawCircles(Canvas canvas) {
		circlePaint = new Paint();
		circlePaint.setDither(true);
		circlePaint.setColor(Color.BLACK);  // alpha.r.g.b
		circlePaint.setStyle(Paint.Style.STROKE);
		circlePaint.setStrokeJoin(Paint.Join.ROUND);
		circlePaint.setStrokeCap(Paint.Cap.ROUND);
		circlePaint.setStrokeWidth(2);

		int numCircles = screenWi / 100;
		int circleDia = screenWi / numCircles;
		for (int i = 0; i < numCircles; i++) {
			for (int j = 0; i < numCircles; j++) {
				circleLocations.add(new Point(screenWi - (j * (circleDia / 2)), screenHi - (i * (circleDia / 2))));
			}
		}

		for (Point point : circleLocations) {
			canvas.drawCircle(point.x, point.y, circleDia / 2, circlePaint);
		}
		return canvas;
	}
I am pretty much clueless on how to do this. Also, how to do you get the screenWi and screenHi. I have an onMeasure method that just calls its super. Thanks!!
Posted

1 solution

Call this method in the onDraw function:
protected void onDraw (Canvas canvas) {
          drawCircles(canvas);
}

Every time when your UI is refreshed, the onDraw function will be called. To trigger the UI refresh. Just invalidate your custom view. For example
Java
mCustomerView.invalidate();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900