Csabi
Csabi

Reputation: 3043

how to draw path into canvas in android?

hi i want to draw path into canvas but it does not do anything, it does not draw the canvas either.
my code:

Path path = new Path();
Canvas canvas = new Canvas();

Paint paint = new Paint();
paint.setColor(Color.BLACK);

canvas.drawColor(Color.CYAN);

for (int i = 5; i < 50; i++)
{
    path.setLastPoint(4, i - 1);
    path.lineTo(4, i);
}
path.close();
for (int i = 0; i < 5; i++)
{
    View iview = inflater.inflate(R.layout.linear_layout, null);
    iview.findViewById(R.id.imageView1);//.setBackgroundColor(backgroundColors[i]);

    ShapeDrawable mDrawable = new ShapeDrawable(new OvalShape());
    mDrawable.getPaint().setColor(Color.YELLOW);
    mDrawable.setBounds(10, 10, 15, 15);

    canvas.drawPath(path, paint);
    mDrawable.draw(canvas);

    iview.draw(canvas);
    realViewSwitcher.addView(iview);
}

it shows me only green color which is the default value of the view.backroundColor.

thanks

Upvotes: 2

Views: 9495

Answers (1)

bigstones
bigstones

Reputation: 15267

Canvas is just a "tool" for drawing over a Bitmap. You need to associate them this way:

Canvas canvas = new Canvas(someBitmap);

However this only lets you draw on someBitmap. If you want to draw in the view, you have to do that in onDraw(Canvas), using the Canvas you're provided with, which is already bound with the bitmap that is the real view's drawing.

To use onDraw(), you have to make a custom View (that is, extend from some View).

Then there is SurfaceView but that's another thing (and I don't know much about it).

Upvotes: 8

Related Questions