Reputation: 139
Which code should I put in the comment in the following OnTouch
function so that the image, CIRCLE1, moves to the right or left when the right or left part of the screen is touched? As you can see I tried to use
float X = ImageView.getX(); X++;
How could I improve this?
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.circle1:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//WHAT CODE SHOULD I PUT INSTEAD OF THE FLOAT X AND X++
float X = ImageView.getX();
X++;
}
break;
}
return false;
}
Upvotes: 1
Views: 44
Reputation: 10910
If you just want it to move right or left, rather than follow the user touch, you could also use an animation rather than adjusting the layout parameters. For example, to just move the view right or left if the user touches the right or left half of the screen, you could use:
int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
float Xtouch = event.getRawX();
int sign = Xtouch > 0.5*ScreenWidth ? 1 : -1;
float XToMove = 50; // or whatever amount you want
int durationMs = 50;
v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
Upvotes: 1