如何在android中将路径绘制到canvas中?

发布于 2024-11-06 10:23:01 字数 845 浏览 1 评论 0原文

嗨,我想将路径绘制到画布中,但它不执行任何操作,它也不绘制画布。
我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2024-11-13 10:23:01

Canvas 只是一个用于在Bitmap 上绘图的“工具”。您需要以这种方式关联它们:

Canvas canvas = new Canvas(someBitmap);

但是,这只允许您在 someBitmap 上绘制。如果您想在视图中绘制,则必须在 onDraw(Canvas) 中使用您提供的 Canvas 来执行此操作,该画布已与位图绑定那是真实的视图的图画。

要使用onDraw(),您必须创建一个自定义View(即,从某个View扩展)。

然后还有 SurfaceView,但那是另一回事(我对此了解不多)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文