onDraw 操作没有将圆放在我期望的位置
我正在尝试进行简单的 GUI 交互 - 触摸屏幕并使红球从之前的位置移动到那里。一切都很好,除了我的球出现在偏离我预期的位置之外。偏移量与我绘制原始球的位置直接相关。就好像球视图将偏移量作为其大小的一部分。 例如,如果我在 X=100、Y=100、半径为 25 处绘制原图,则稍后必须从每个 getX 和 getY 返回值中减去 100,以将其放置在正确的位置。我确信这不正常,但无法发现我的问题。有人可以帮忙吗?该代码
public LinearLayout my_canvas;
public Ball ball1;
public class Ball extends View {
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
LinearLayout.LayoutParams tlp =
new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT );
this.setLayoutParams(tlp);
this.x = x;
this.y = y;
this.r = r;
Log.v("BookOne", "ball constructor at "+x+","+y);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
my_canvas.invalidate();
}
}
位于我的主应用程序中。接下来是
my_canvas = (LinearLayout) findViewById(R.id.my_canvas);
my_canvas.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
Log.v("BookOne", "Entered onTouch for canvas");
float x = e.getX();
float y = e.getY();
Log.v("BookOne", "set coordinates");
ball1.setX(x);
ball1.setY(y);
ball1.invalidate();
//my_canvas.invalidate();
Log.v("BookOne", "get ranges");
int r = v.getRight();
int l = v.getLeft();
int t = v.getTop();
int b = v.getBottom();
int w = v.getWidth();
int h = v.getHeight();
Log.v("BookOne", "Ball moving to "+x+","+y);
log_view_dimensions("ball1 dimensions", ball1);
log_view_dimensions("View v dimensions", v);
log_view_dimensions("my_canvas dimensions", my_canvas);
Log.v("BookOne", "about to return FALSE");
return(false);
}
});
ball1 = new Ball(my_canvas.getContext(), 100, 100, 25);
my_canvas.addView(ball1);
Log.v("BookOne", "ball1 added");
我从 getX 和 getY 获得的 X 和 Y 值看起来正确,但球正在重新绘制 100 像素之外。
仅供参考,这是 LogCat 的转储:
07-26 20:54:43.830: VERBOSE/BookOne(5463): Entered onTouch for canvas 07-26 20:54:43.830: VERBOSE/BookOne(5463): 设置坐标 07-26 20:54:43.830:VERBOSE/BookOne(5463):获取范围 07-26 20:54:43.830: VERBOSE/BookOne(5463): 球移动到 337.0,262.5 07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 尺寸 l=0; r=800; t=0; b=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 尺寸 w=800;高=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): 查看 v 尺寸 l=0; r=800; t=584; b=1120 07-26 20:54:43.830: VERBOSE/BookOne(5463): 查看 v 尺寸 w=800;高=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas 尺寸 l=0; r=800; t=584; b=1120 07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas 尺寸 w=800;高=536 07-26 20:54:43.830: VERBOSE/BookOne(5463): 即将返回 FALSE
请注意 ball1 上的尺寸。有什么影响?
非常感谢您的帮助。
麦克风
I am trying to do a simple GUI interaction - touch the screen and have a red ball move there from its previous position. Everything works great, except that my ball appears at an offset from where I intended. The offset is directly related to the where I drew the original ball. It's as if the ball view includes that offset as part of its size.
For example, if I draw the original at X=100, Y=100 with radius 25, I later have to subtrace 100 from each of my getX and getY return values to get it placed in the proper place. I'm sure this isn't normal, but haven't been able to spot my problem. Can someone help? The code is
public LinearLayout my_canvas;
public Ball ball1;
public class Ball extends View {
private final float x;
private final float y;
private final int r;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
public Ball(Context context, float x, float y, int r) {
super(context);
mPaint.setColor(0xFFFF0000);
LinearLayout.LayoutParams tlp =
new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT );
this.setLayoutParams(tlp);
this.x = x;
this.y = y;
this.r = r;
Log.v("BookOne", "ball constructor at "+x+","+y);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
my_canvas.invalidate();
}
}
is within my main application. Followed by
my_canvas = (LinearLayout) findViewById(R.id.my_canvas);
my_canvas.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
Log.v("BookOne", "Entered onTouch for canvas");
float x = e.getX();
float y = e.getY();
Log.v("BookOne", "set coordinates");
ball1.setX(x);
ball1.setY(y);
ball1.invalidate();
//my_canvas.invalidate();
Log.v("BookOne", "get ranges");
int r = v.getRight();
int l = v.getLeft();
int t = v.getTop();
int b = v.getBottom();
int w = v.getWidth();
int h = v.getHeight();
Log.v("BookOne", "Ball moving to "+x+","+y);
log_view_dimensions("ball1 dimensions", ball1);
log_view_dimensions("View v dimensions", v);
log_view_dimensions("my_canvas dimensions", my_canvas);
Log.v("BookOne", "about to return FALSE");
return(false);
}
});
ball1 = new Ball(my_canvas.getContext(), 100, 100, 25);
my_canvas.addView(ball1);
Log.v("BookOne", "ball1 added");
The X and Y values I am getting from getX and getY appear correct, but the ball is being redraw 100 pixels away.
FYI, here's the dump from LogCat:
07-26 20:54:43.830: VERBOSE/BookOne(5463): Entered onTouch for canvas
07-26 20:54:43.830: VERBOSE/BookOne(5463): set coordinates
07-26 20:54:43.830: VERBOSE/BookOne(5463): get ranges
07-26 20:54:43.830: VERBOSE/BookOne(5463): Ball moving to 337.0,262.5
07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 dimensions l=0; r=800; t=0; b=536
07-26 20:54:43.830: VERBOSE/BookOne(5463): ball1 dimensions w=800; h=536
07-26 20:54:43.830: VERBOSE/BookOne(5463): View v dimensions l=0; r=800; t=584; b=1120
07-26 20:54:43.830: VERBOSE/BookOne(5463): View v dimensions w=800; h=536
07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas dimensions l=0; r=800; t=584; b=1120
07-26 20:54:43.830: VERBOSE/BookOne(5463): my_canvas dimensions w=800; h=536
07-26 20:54:43.830: VERBOSE/BookOne(5463): about to return FALSE
Notice the dimensions on ball1. What are the implications?
Many thanks in advance for any help.
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
圆偏移的可能原因是您没有重写 onMeasure() 方法。
如果未提供 onMeasure 的实现,则默认情况下 Android 会将大小设置为 100 X 100。这就是您最终从 x 和 y 中减去 100 以使球位于正确位置的原因。
The possible reason for the offset of your circle is that you have not overridden the onMeasure() method.
If an implementation of onMeasure is not provided then by default Android will set the size to 100 X 100. That's the reason you end up subtracting 100 from x and y to get the ball at the correct location.