Note that there are some explanatory texts on larger screens.

plurals
  1. POMotionEvent - How to get back to home position?
    primarykey
    data
    text
    <p>At the beginning I'm not quite good in english so please forgive me any mistakes.</p> <p>I made simple app which moves a white circle from home position (100,100) to any other in limited RelativeLayout (200x200). All I want to do is to move that circle to home position (100,100) after I stop touching screen. I'm using MotionEvent actions but when I set anything in ACTION_CANCEL or ACTION_UP (for example x=100; y=100;) it move circle even when I'm still touching or moving it. </p> <p>I will be very grateful for any help.</p> <p>code:</p> <pre><code>public class Dott extends View { private static final float RADIUS = 20; TextView X; float x = 100; float y = 100; float Pressure1, Pressure2; public static float kurwaX, kurwaY; private float initialX; private float initialY; private float offsetX; private float offsetY; private Paint backgroundPaint; private Paint myPaint; public Dott(Context context, AttributeSet attrs) { super(context, attrs); backgroundPaint = new Paint(); backgroundPaint.setColor(Color.BLUE); myPaint = new Paint(); myPaint.setColor(Color.WHITE); myPaint.setAntiAlias(true); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: x=event.getX(); y=event.getY(); initialX = x; initialY = y; offsetX = event.getX(); offsetY = event.getY(); break; case MotionEvent.ACTION_MOVE: x = initialX + event.getX() - offsetX; y = initialY + event.getY() - offsetY; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: break; } return(true); } @Override public void draw(Canvas canvas) { int width = canvas.getWidth(); int height = canvas.getHeight(); canvas.drawRect(0,0,width,height, backgroundPaint); if (x&gt;=200) { x=200; } else if (x&lt;=0) { x=0; } if (y&gt;=200) { y=200; } else if (y&lt;=0) { y=0; } canvas.drawCircle(x, y, RADIUS, myPaint); invalidate(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload