Android return to the original position of the view after MotionEvent

Posted by Kurty on Stack Overflow See other posts from Stack Overflow or by Kurty
Published on 2012-03-18T17:59:35Z Indexed on 2012/03/18 18:02 UTC
Read the original article Hit count: 234

Filed under:
|
|
|

My application currently changes to another random int (View) when I let go of it (ACTION_UP) but the view stays in the same spot where I dropped it. I want it to return to the original location (the middle of the screen) when I drop it so I can repeat the process.

// OnTouch and MotionEvent
OnTouchListener dragt = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent me) {
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) v.getLayoutParams();

switch (v.getId()) {
case R.id.randomView:
}
switch(me.getAction()) {    
case MotionEvent.ACTION_MOVE:
    par.gravity = 0;
    par.setMargins((int)me.getRawX() - (v.getWidth())/2, (int)me.getRawY() - (v.getHeight())/2, 0, 0);
    v.setLayoutParams(par);
    break;

case MotionEvent.ACTION_UP:
    // tallies score.
    score++;
    textScore.setText(String.valueOf(score));

    // this generates the new view but the location is still the same
    color.setImageResource(mImageIds[rgenerator.nextInt(mImageIds.length)]);
    break;
        }
return true;
    }
};

© Stack Overflow or respective owner

Related posts about java

Related posts about android