-3

How to take first touch coordinates and make it constant. I wanna click something on screen then draw a permanent on first touch and then want to move another circle within the the permanent circle one

hers the code am trying :

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;
}
}

1 Answers1

0

Override onTouchEvent(MotionEvent event) and then call event.getX() and event.getY() to get the coordinate positions of where the user touched.

then store value to some variable.

now use these values you want and try replacing to another.

Check stack answer to get touch screen information answer

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142