I have the following onDraw
:
public void onDraw(Canvas canvas) {
paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(rectangulo,paint);
}
I would like to know how to implement so that it was not just Color.RED
random colors.
Something like an array with various colors and the set color calls it?
This is my code:
private static final int[] colores = {Color.GREEN,Color.BLUE,Color.RED};
In the constructor:
r = new Random();
num = r.nextInt(3);
and the Ondraw:
public void onDraw(Canvas canvas) {
paint = new Paint();
paint.setColor(colores[num]);
canvas.drawRect(rectangulo,paint);
}
The simplest for random colors is:
Leaving your method:
show
Another way is to create each value of
RGB
individually.show
Note: You have to import
java.util.Random
You can create random colors without using Paint
For that you can use this method:
Which is used in this example: Buttons in Android . To call the method, you simply do it like this:
so that each time the method
onDraw()
is called, it paints a random color.