Reputation: 2709
I want the program to draw a circle whenever the screen gets touched and if the screen gets touched on another position I want the program to draw a circle again but without deleting the old one!
Now my problem is that it doesn't just draw a new circle in addition to the old one. It draws a new circle and deletes the old one. I tried to find a solution but nothing worked.
So can anyone please help me?
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
public class SingleTouchEventView extends View {
private Paint paint = new Paint();
List<Point> points = new ArrayList<Point>();
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.GREEN);
for(Point p: points){
canvas.drawCircle(p.x, p.y, 20, paint);
}
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Point p = new Point();
p.x = (int)event.getX();
p.y = (int)event.getY();
points.add(p);
invalidate();
case MotionEvent.ACTION_MOVE: // a pointer was moved
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
break;
}
}
invalidate();
return true;
}
}
Upvotes: 2
Views: 4112
Reputation: 470
You can achieve it by maintaining a list of points as a private instance variable:
private List<Point> points = new ArrayList<Point>;
Then you can add new points to this list every time a new touch event occurs:
Point p = new Point();
p.x = event.getX();
p.y = event.getY();
points.add(p);
invalidate();
Now in the onDraw() method you can print all the points in the list:
paint.setColor(Color.GREEN);
for(Point point: points){
canvas.drawCircle(point.x, point.y, 20, paint);
}
invalidate();
Upvotes: 6
Reputation: 1712
one of my changing circle classes:
public class GrowCircle {
float x, y;int radius;
Paint myp = new Paint();
int colr,colg,colb;
int redvar=1;
int bluevar=5;
int greenvar=2;
int tripper=10;
int change=2;
Random rand = new Random();
public GrowCircle(float x, float y){
this.x=x;
this.y=y;
this.radius=2;
this.colr=rand.nextInt(254)+1;
this.colg=rand.nextInt(254)+1;
this.colb=rand.nextInt(254)+1;
}
public void update(){
if(tripper<=1||tripper>=15){
change=-change;
}
Random col = new Random();
myp.setColor(Color.argb(255,colr,colg,colb));
colr+=redvar;
colg+=greenvar;
colb+=bluevar;
if(colr<=5||colr>=250){
redvar=-redvar;
}
if(colg<=5||colg>=250){
greenvar=-greenvar;
}
if(colb<=5||colb>=250){
bluevar=-bluevar;
}
}
public void drawThis(Canvas canvas){
myp.setStrokeWidth(tripper);
myp.setStyle(Style.STROKE);
canvas.drawCircle(x, y, radius, myp);
}
}
so play with that and make it how you want and then do this:
///var declaration
CopyOnWriteArrayList<GrowCircle> gcirc= new CopyOnWriteArrayList<GrowCircle>();
//in update method
for(GrowCircle circ:gcirc){
circ.update();
}
// in draw method
for(GrowCircle circ:gcirc){
circ.drawThis(c);
}
//and in onTouch
float tx =event.getX();
float ty = event.getY();
gcirc.add(new GrowCircle(tx,ty));
Upvotes: 1
Reputation: 382
Why not make a circle object which stores the coordinates of where to paint the circle, then add these objects to an array. Then in your paint method iterate through the array get the coordinates from each object and paint the circle using the coordinates?
So in your onclick method create a new circle object with the coordinates gained from the touch
Upvotes: 1