-
-
Save mshi/8287fd3956c9a917440d to your computer and use it in GitHub Desktop.
drawing random circles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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