您只需要一个
Random实例。
声明
private long lastUpdated = 0;和
private int lastColor =Color.BLACK;对的onDraw之外。
将底部更新为:
final float radius = 230f;if(System.currentTimeMillis() > lastUpdated + 1000){ lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN; lastUpdated = System.currentTimeMillis();}paint.setColor(lastColor);canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);这将每秒在随机位置绘制一个红色或绿色的圆圈。
您需要半径为2,因为坐标是从圆心开始的。
至于您的问题的第二部分, 我也想在旁注中慢慢产生更快的冷静 。您必须澄清您的意思。
编辑:在此处提供了更完整(更正确)的示例:https
:
//gist.github.com/mshi/8287fd3956c9a917440d



