Draw path in background with respect to shape - android

I'm going through a for loop over elemets of a synchronized ArrayList inside the draw method of a SurfaceView like this:
public void draw(Canvas canvas) {
for(MyObject o : system) {
canvas.drawPath(o.getPath(), mPaint);
canvas.drawCircle(o.getX(), o.getY(), radius, mPaint);
}
}
The problem is that this will draw next elements in the list on top of previous elements. There is a way to make the paths drawn behind all circles without proliferation of for loops?

Related

Xamarin Android: How to draw on top of Canvas

I am writing a custom renderer for Xamarin.Forms. I am overriding the class ImageRenderer to do some tweaks to the image.
However, I want to overlay some parts of the image with circles, so I override the method void OnDraw(Canvas canvas).
Then I modify the Canvas a bit by drawing some circles:
protected override void OnDraw(Canvas canvas)
{
var paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
foreach (var mapObject in _control.PointSource)
{
canvas.DrawCircle(mapObject.Location.X, mapObject.Location.Y, 100 / _scaleFactor, paint);
}
base.OnDraw(canvas);
}
However, everything is drawn on the background. The actual image is always on top. How can one draw circles on top of the image?
https://developer.xamarin.com/api/member/Android.Views.View.OnDraw/
In the docs they say the following:
the canvas on which the background will be drawn
So the current behaviour was expected.
I managed to fix it by moving the circle logic to the function: void Draw(Canvas canvas).

Android - redraw canvas

I have 3 ImageViews. 1st and 2nd connected with red line. Also I have simple button. Here is a picture:
I want to connect 2nd & 3rd ImageViews with new Path line and change the first line color (for example to Green) when i clicking my button. Here is parts of my code:
public class SkillPath extends View {
Paint paint;
Path path;
... constructors
#Override
protected void onDraw(Canvas canvas) {
addPath (canvas);
}
//Here is my RED line
void addPath (Canvas canvas){
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
path.moveTo(110, 110);
path.lineTo(210, 110);
canvas.drawPath(path, paint);
Log.d ("Page 2","onDraw");
}
I can get all coordinates of all Views, but how can I redraw existing canvas? I suspect, I need to use invalidate(), but I do not know enough to do this. Need help.
The invalidate() method forces the View to be re-drawn.
So just apply the modification you need on your canvas and call invalidate() on the related View after these modifications.

Insert Drawable shape to View

I would like to send each time different Drawable shape to view and then add it to my activity.
The problem is I can't find a way to add the drawable shape into the canvas when I'm overriding the onDraw method.
The design supposed to be reusable, I can draw rect first and then draw circle...
I want to find a way to send the view different shape. Is it possible?
#Override
protected void onDraw(Canvas canvas) {
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
// canvas.drawOval(ballBounds, paint);
canvas.drawRect(ballBounds, paint);
}
Although, your question needs more explanation on what you need, but with what I could understand is you want rect, or circle on desire, here it is,
Add, global variable int ShapeStructure = 0;
#Override
protected void onDraw(Canvas canvas)
{
// Draw the ball
ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius);
paint.setColor(Color.GREEN);
if(ShapeStructure == 0){
canvas.drawOval(ballBounds, paint);
}else{
canvas.drawRect(ballBounds, paint);
}
}
Now that, you have a variable at your disposal, you can control what you want to paint.
Hope, this helps, let me know if you want something else.

canvas not redrawing correctly

I am writing a simple view that will show a dot, after some action, show the next dot. The dots are on a vertical strip. so I create a function that'll clear the strip. then draw a circle at the position where I want the dot. the code segment is as followed.
private void clear_strip(){
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
m_canvas.drawRect( 0, 0,width/8, height, paint);
paint.setColor(Color.GREEN);
}
private void set_dot(){
clear_strip();
m_canvas.drawCircle(width/10, (int) (font_height*(scoreboard.current_batter_position()+0.5))/1, font_height/4, paint);
}
#Override
protected void onDraw(Canvas canvas) {
set_dot();
canvas.drawBitmap(m_bitmap, 0, 0, paint);
}
but one of the dot is just not updating. It'll keep the old dot, skip that dot, then move to the next dot. I tried to print out the position to logcat right before the drawCircle call, and the position is right, it's just not drawing (and not clearing as well)....please advise.
You get a canvas to draw into passed into your onDraw method. But the point drawing code uses the canvas m_canvas. Pass the canvas as a parameter to your dot drawing code to fix that.

Android Animate canvas.drawLine

I'm trying to draw a few lines on a DrawView which extends SurfaceView.
in my onDraw method I loop over a list of float arrays and draw lines.
#Override
public void onDraw(final Canvas canvas) {
synchronized (lines) {
super.onDraw(canvas);
for (float[] l : lines) {
paint.setAntiAlias(true);
canvas.drawLines(l, paint);
}
}
}
I would like to animate each of these lines. I've tried to use ViewAnimator even though it's only Honeycomb and above but either I don't understand how to use it or it just isn't meant to be used with canvas.drawLine(). I've tried to use paint.setPathEffect but it doesn't seem to work at all. Anyone have any idea how to do this? I just want each line to take something like 1 second to draw one after the other.

Categories

Resources