Skip to content

Instantly share code, notes, and snippets.

@mshi
Created March 29, 2015 19:03
Show Gist options
  • Save mshi/8287fd3956c9a917440d to your computer and use it in GitHub Desktop.
Save mshi/8287fd3956c9a917440d to your computer and use it in GitHub Desktop.
drawing random circles
private int lastColor = Color.BLACK;
private final Random random = new Random();
private final Paint paint = new Paint();
private final int radius = 230;
private final Handler handler = new Handler();
private final Runnable updateCircle = new Runnable() {
@Override public void run() {
lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
paint.setColor(lastColor);
invalidate();
handler.postDelayed(this, 1000);
}
};
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
handler.post(updateCircle);
}
@Override protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
handler.removeCallbacks(updateCircle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// your other stuff here
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment