我正在开发一个用于在两点之间绘制线条的应用程序。例如:
我有这样的地图(请想象X是点):
X X X X X X X X X X
每个人都是用这句话创建的:
canvas.drawCircle(50,100,15,paint);
canvas.drawCircle(148,100,15,paint);
canvas.drawCircle(246,100,15,paint);
canvas.drawCircle(344,100,15,油漆);
canvas.drawCircle(442,100,15,油漆);
canvas.drawCircle(540,100,15,油漆);
canvas.drawCircle(638,100,15,油漆);
canvas.drawCircle(736,100,15,油漆);
canvas.drawCircle(834,100,15,油漆);
canvas.drawCircle(932,100,15,油漆);
我使用监听器来捕捉用户交互的位置
案例MotionEvent.ACTION_UP:
downx = event.getX();
downy = event.getY();
问题是当我在点(932,100)和(832,100)之间进行clic时,X位置返回771并且它不是832和932之间的值。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int PMaxX=0;
int PMaxY =0;
final ArrayList<ArrayList<Integer>> Matriz = new ArrayList<ArrayList<Integer>>();
if (android.os.Build.VERSION.SDK_INT >= 13) {
display.getSize(size);
PMaxX = size.x;
PMaxY = size.y;
} else if (android.os.Build.VERSION.SDK_INT < 13) {
// Log.d("State", "Prueba menor 13");
PMaxX = display.getWidth();
PMaxY = display.getHeight();
}
Bitmap bitmap = Bitmap.createBitmap(PMaxX, PMaxY, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.rgb(0, 0, 0));
paint.setStrokeWidth(10);
canvas.drawCircle(50, 100, 15, paint);
canvas.drawCircle(148, 100, 15, paint);
canvas.drawCircle(246, 100, 15, paint);
canvas.drawCircle(344,100,15,paint);
canvas.drawCircle(442,100,15,paint);
canvas.drawCircle(540,100,15,paint);
canvas.drawCircle(638,100,15,paint);
canvas.drawCircle(736,100,15,paint);
canvas.drawCircle(834,100,15,paint);
canvas.drawCircle(932,100,15,paint);
canvas.drawCircle(1065,100,15,paint);
drawingImageView.setImageBitmap(bitmap);
drawingImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_UP:
downx = event.getX();
downy = event.getY();
Log.d("State","Show X" + Float.toString(downx));
Log.d("State","Show Y" + Float.toString(downy));
break;
default:
break;
}
return true;
}
});
}