画线-Android
我想使用触摸监听器在屏幕上画一条线,但是当我尝试再次画线时,它会删除前一行。我正在使用这段代码:-
我无法找到问题的解决方案。
public class Drawer extends View
{
public Drawer(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
Paint p = new Paint();
p.setColor(colordraw);
canvas.drawLine(x1, y1, x2, y2, p);
invalidate();
}
}
I want to draw a line on screen using touch listener, but when i try to draw line again it erases the previous line. I am using this code:-
I am unable to find a solution to the problem.
public class Drawer extends View
{
public Drawer(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
Paint p = new Paint();
p.setColor(colordraw);
canvas.drawLine(x1, y1, x2, y2, p);
invalidate();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定 invalidate() 会擦除画布,因此您必须保留要绘制的线条的集合。然后你需要在调用 invalidate() 之前每次都绘制它们。
I'm pretty sure that invalidate() wipes the canvas, so you have to keep a collection of lines that you want to draw. Then you need to draw ALL of them EVERY time before calling invalidate().