如何在android中将路径绘制到canvas中?
嗨,我想将路径绘制到画布中,但它不执行任何操作,它也不绘制画布。
我的代码:
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);
}
它只显示绿色,这是 view.backroundColor 的默认值。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Canvas
只是一个用于在Bitmap
上绘图的“工具”。您需要以这种方式关联它们:但是,这只允许您在 someBitmap 上绘制。如果您想在视图中绘制,则必须在
onDraw(Canvas)
中使用您提供的Canvas
来执行此操作,该画布已与位图绑定那是真实的视图的图画。要使用
onDraw()
,您必须创建一个自定义View(即,从某个View
扩展)。然后还有 SurfaceView,但那是另一回事(我对此了解不多)。
Canvas
is just a "tool" for drawing over aBitmap
. You need to associate them this way: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 theCanvas
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 someView
).Then there is
SurfaceView
but that's another thing (and I don't know much about it).