我在这里遇到了奇怪的问题。当用户单击 View 时,我添加到数组列表。我获取位置并将其添加到坐标 ArrayList 中。然后,我在 Canvas 上按照坐标指示绘制一个圆圈。 onDraw 中的 Size 检查始终返回 0。
private static ArrayList<Coordinate> coords = new ArrayList<Coordinate>();
触摸事件
...
case MotionEvent.ACTION_POINTER_UP: {
mLastTouchX = event.getX();
mLastTouchY = event.getY();
this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
break;
...
绘图
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(int i = 0; i < this.coords.size(); i++) {
canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
}
}
最佳答案
您希望如何使用 this
获得静态字段?但假设这只是一些拼写错误,请尝试添加一些日志记录:
case MotionEvent.ACTION_POINTER_UP: {
mLastTouchX = event.getX();
mLastTouchY = event.getY();
this.coords.add(new Coordinate(mLastTouchX, mLastTouchY));
System.out.println("Added a coordinate; new size: " + coords.size());//to see if we are adding it
break;
在你的 onDraw 中:
System.out.println(coords);//Just to see what all is in it
for(int i = 0; i < this.coords.size(); i++) {
canvas.drawCircle(coords.get(i).x, coords.get(i).y, 30.0f, mPaint);
}
关于java - Android 和 ArrayLIst Size 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103460/