如何获取当前画布?
我有绘图视图。如果我触摸这个视图,它会画出小圆圈。我不会画圆圈,但不会触摸视图 - 使用帮助功能“setPoints”。我做什么:
package com.samples;
import ...
public class DrawView extends View {
ArrayList<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
private int pSize = 5;
private int pColor = Color.BLACK;
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setOnTouchListener(this);
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
}
return true;
}
});
requestFocus();
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, pSize, paint);
}
}
public void setPoints(Float xP, Float yP)
{
Point point = new Point();
point.x = xP;
point.y = yP;
points.add(point);
postInvalidate();
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
请告诉我,如何获取canvas的setPoints函数?
更新: 哇,这真是一个有趣的问题。我的 DrawView 包含在 HorizontalScrollView 中。因为如果我在这个 DrawView 中设置右坐标,没有人知道可绘制的圆圈在哪里。
I have DrawView. If I touch this view it draws small circles. I wont to draw circles but not to touch view - with help function "setPoints". What I do:
package com.samples;
import ...
public class DrawView extends View {
ArrayList<Point> points = new ArrayList<Point>();
Paint paint = new Paint();
private int pSize = 5;
private int pColor = Color.BLACK;
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setOnTouchListener(this);
Point point = new Point();
point.x = event.getX();
point.y = event.getY();
points.add(point);
invalidate();
}
return true;
}
});
requestFocus();
}
@Override
public void onDraw(Canvas canvas) {
for (Point point : points) {
canvas.drawCircle(point.x, point.y, pSize, paint);
}
}
public void setPoints(Float xP, Float yP)
{
Point point = new Point();
point.x = xP;
point.y = yP;
points.add(point);
postInvalidate();
}
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
Please tell me, how get canvas out setPoints function?
Update:
Wow, it's really interesting problem. My DrawView contains in HorizontalScrollView. Because if I set in this DrawView right coordinates, no one knows where are drawable circles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不能。画布由系统管理并传递给您的
onDraw()
。我不明白为什么你需要它在外面。只需像这样重新声明setPoints
您就可以保留以前绘图的缓存(或存储以前的点)
You can't. The canvas is managed by the system and is passed to your
onDraw()
. I don't understand why you'd need it outside of there. Just redeclaresetPoints
like thisYou can keep a cache of the previous drawings (or store the previous points)
尝试将 canvas2 声明为 DrawView 类中的公共变量。
Try declaring canvas2 as a public variable in the DrawView class.
您可以在
onDraw()
中绘制圆圈。这就是View
应该工作的方式(从技术上讲,它实际上是在draw()
方法中,但我们会忽略这一点)。在setPoints()
中,在类范围内的变量中设置圆的点,调用invalidate()
,然后像在onDraw()中那样绘制圆
。如果您遵循此方法,您就遵循了视图设计的类流程。You draw your circles in
onDraw()
. That's the wayView
is supposed to work (technically it's actually in thedraw()
method but we'll overlook that). InsetPoints()
, set the points of the circle in variables within the class scope, callinvalidate()
, then draw the circle like that inonDraw()
. If you follow this method, you're following the class flow that the view was designed for.