Reputation: 467
I'm trying to code an imageView click feedback using the onTouch method. My code is used to scale the imageView when pressed(MotionEvet.ACTION_DOWN
) and return to its normal size when the user stop pressing(MotionEvet.ACTION_UP
). But what I'm not able to code is the action when the user drags its finger out of the imageView.
I've seen a solution which tells to use MotionEvent.ACTION_CANCEL
at the beginning of the switch statement, but this doesn't work for me.
My code is the next one:
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_CANCEL:
clickOutTransformation(ico);
return true;
case MotionEvent.ACTION_UP:
clickOutTransformation(ico);
switch (i) {
case 1:
fondoApp.setBackgroundResource(R.drawable.back_blue_bubbles_lite);
i++;
break;
case 2:
fondoApp.setBackgroundResource(R.drawable.back_espectrum);
i++;
break;
case 3:
fondoApp.setBackgroundResource(R.drawable.back_black_and_violet);
i++;
break;
case 4:
fondoApp.setBackgroundResource(R.drawable.back_green);
i++;
break;
case 5:
fondoApp.setBackgroundResource(R.drawable.back_blur_blue_ed);
i = 1;
break;
default:
break;
}
return true;
case MotionEvent.ACTION_DOWN:
clickInTransformation(ico);
return true;
default:
break;
}
return false;
}
Upvotes: 0
Views: 1144
Reputation: 1439
you can get it with this solution:
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
// User moved outside bounds
}
break;
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
}
return false;
}
Upvotes: 1
Reputation: 2178
Use if case: MotionEvent.ACTION_DOWN for scaling and case MotionEvent.ACTION_UP: for scaling down to normal size ...
ACTION_DOWN fires, when you touch you screen and ACTION_UP fires, when you take off you finger from view
Upvotes: 0