try this Code:
package com.cal;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class CircleCanvas extends View
{
public List<circleinfo> mCircleInfos = new ArrayList<circlecanvas.circleinfo>();
public static class CircleInfo
{
private float x;
private float y;
private float radius;
private int color;
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getRadius()
{
return radius;
}
public void setRadius(float radius)
{
this.radius = radius;
}
public int getColor()
{
return color;
}
public void setColor(int color)
{
this.color = color;
}
}
public CircleCanvas(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for (CircleInfo circleInfo : mCircleInfos)
{
Paint paint = new Paint();
paint.setColor(circleInfo.getColor());
canvas.drawCircle(circleInfo.getX(), circleInfo.getY(), circleInfo.getRadius(), paint);
}
}
}
</circlecanvas.circleinfo></circleinfo>
and use it in the Activity ,eg in btn clicked:
just like this:
listener3 = new OnClickListener() {
public void onClick(View v) {
Random random = new Random();
float randomX =( 100 + random.nextInt(100));
float randomY =( 100 + random.nextInt(100));
float randomRadius =( 20 + random.nextInt(40));
int randomColor = 0;
if(random.nextInt(100) > 50)
{
randomColor = Color.BLUE;
}
else
{
if(random.nextInt(100) > 50)
randomColor = Color.RED;
else
randomColor = Color.GREEN;
}
CircleInfo circleInfo = new CircleInfo();
circleInfo.setX(randomX);
circleInfo.setY(randomY);
circleInfo.setRadius(randomRadius);
circleInfo.setColor(randomColor);
mCircleCanvas.mCircleInfos.add(circleInfo);
mCircleCanvas.invalidate();
}
};