Draw Circle on Touch

Draw Circle on touch

I would do it this way:

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawingView(this));
}

class DrawingView extends SurfaceView {

private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

public DrawingView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawCircle(event.getX(), event.getY(), 50, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
}

Draw new Circle with each new Touch Event

The line

canvas.drawARGB(255,255,255,255);

keeps drawing over the rendered circle. Removing it means that each new circle is visible on the canvas.

Draw a circle each time I touch the screen

Try this one. Clear the imageView's bitmap inside the action ACTION_UP. Here is the example code:

First make your bitmap outside the onTouchListener like:

private var bitmap: Bitmap? = null

Then your onTouchListener will be like:

imageBody.setOnTouchListener({ v, event ->

if(event != null){
if (event.action == MotionEvent.ACTION_DOWN) {
bitmap = Bitmap.createBitmap(imageBody.drawingCache)
val pixel: Int = bitmap.getPixel(event.getX().toInt(), event.getY().toInt())
coordX = event.getX()
coordY = event.getY()

val Drawable = imageBody.drawable
val ImageBounds = Drawable.bounds
val scaledHeight = ImageBounds.height()
val scaledWidth = ImageBounds.width()

OrigX = coordX / scaledHeight
OrigY = coordY / scaledWidth

when (pixel) {
Color.rgb(241,241,241) -> {
val canvas = Canvas(bitmap)
val paint = Paint()
paint.color = Color.rgb(255,128,0)

canvas.drawCircle(coordX, coordY, 15F, paint) /**DRAW CIRCLE*/

imageBody.setImageBitmap(bitmap)
imageBody.Invalidate()
}
}
}

if (event.action == MotionEvent.ACTION_UP) {
if (bitmap != null && !bitmap!!.isRecycled) {
bitmap?.recycle()
bitmap = null
}
}
}
true
})

Drawing a circle on HTML5 canvas at touch location

Use clientX/Y instead and use bounding rect to compensate for canvas position:

function drawSymptomCircle(event) {

var canvas = this; // no need to get canvas element from DOM, this = canvas

if (canvas.getContext){ // this test should be done once in global, not here :)
...

var rect = this.getBoundingClientRect(); // absolute position of canvas
x = event.targetTouches[0].clientX; // relative to client win
y = event.targetTouches[0].clientY;

x -= rect.left; // adjusted to be relative to canvas
y -= rect.top;

...

Also obtaining getContext() is something I would recommend you do in global/parent scope. this will represent the canvas element as the element is the default context for an event handler.

Draw circle when touched and when touch is removed in a custom view

if you are trying to draw multiple circles when the user touches the screen and touch removed, you need to use a List of circles. you can have an ExampleCircle class where it have an x,y and color field.

public class ExampleCircle{

int x,y;
Color color;
//setters and getters

}

//onTouch
//assuming you already have a List object (List circleList=new ArrayList(); )

ExampleCircle newCircle=new ExampleCircle();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
newCircle.setX((int)event.getX());
newCircle.setY((int)event.getY()) ;
newCircle.setColor(paint.setColor(Color.BLACK));
break;

case MotionEvent.ACTION_UP:
newCircle.setX((int)event.getX());
newCircle.setY((int)event.getY()) ;
newCircle.setColor(paint.setColor(Color.BLACK));
break;
}

circleList.add(newCircle);
invalidate();
return true;

//on draw method

replace

    canvas.drawCircle(point.x, point.y, 5, paint);

with

for(int i=0;i<circleList.size();i++){
ExampleCircle currentCircle=circleList.get(i);

canvas.drawCircle(currentCircle.getX(), currentCircle.getY(), 5, paint or currentCircle.getColor());

}


Related Topics



Leave a reply



Submit