如何获取第一个触摸坐标并使其保持不变。 我想点击屏幕上的内容然后在第一次触摸时绘制一个永久物,然后想要在永久圆圈内移动另一个圆圈
她正在尝试的代码:
public class MainActivity extends Activity implements OnTouchListener {
private float x;
private float y;
static float lasttouchx;
static float lasttouchy;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCustomPanel view = new MyCustomPanel(this);
ViewGroup.LayoutParams params =
new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
addContentView(view, params);
view.setOnTouchListener(this);
}
private class MyCustomPanel extends View {
public MyCustomPanel(Context context) {
super(context);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
paint.setTextSize(50);
canvas.drawText(" X : " + (int) x + " Y : " + (int) y, canvas.getWidth() - 500, 200, paint);
paint.setStyle(Paint.Style.FILL);
if((x<=1000&& x>=18)&&(y<=1380&&y>=348)){
paint.setColor(Color.BLUE);
canvas.drawCircle(x, y, 100, paint);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(20);
canvas.drawCircle(lasttouchx, lasttouchy, 500, paint);
}
else{}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
int action = event.getActionMasked();
switch (action){
case MotionEvent.ACTION_DOWN:
lasttouchx = event.getX();
lasttouchy = event.getY();
return false;
}
v.invalidate();
return true;
}
}
答案 0 :(得分:0)
覆盖onTouchEvent(MotionEvent event)
,然后拨打event.getX()
和event.getY()
以获取用户触摸位置的坐标位置。
然后将值存储到某个变量。
现在使用您想要的这些值并尝试替换为另一个。