I am trying to create an animation:
Use Case : 1. User swipes/ drags item to right ( horizontally ), item gets added into basket. If he again swipes it adds one more of the same item into the basket. 2. User swipes/ drags item to left ( horizontally ), item gets removes from the basket, if was added before, if there is no item in the basket we leave it as it is.
Drag effect : When user drags, item gets moved along with the finger. As soon as he leave it, items gets back to its position.
Zoom effect : When user clicks on the item, item gets zoomed, letting user know that item has been added into the basket. ( replicated method of right dragged ).
I tried to use OnSwipeTouchListener Class specially for this purpose :
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import PointF;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
PointF DownPT = null;
PointF StartPT = null;
float startX;
float startY;
public OnSwipeTouchListener(Context context , float x, float y) {
gestureDetector = new GestureDetector(context, new GestureListener());
DownPT = new PointF();
StartPT = new PointF();
this.startX = x;
this.startY = y;
}
public void onSwipeLeft() {
}
public void onSwipeRight() {
}
public boolean onTouch(View v, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
v.setX((int) (StartPT.x + mv.x));
v.setY(this.startY);
StartPT = new PointF(v.getX(), v.getY());
break;
case MotionEvent.ACTION_DOWN:
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(v.getX(), v.getY());
break;
case MotionEvent.ACTION_UP:
// Move image back to its original position.
v.setX(this.startX);
v.setY(this.startY);
break;
default:
v.setX(this.startX);
v.setY(this.startY);
break;
}
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
When My method OnTouch is like below, my onSwipeRight() and onSwipeLeft() gets triggered but when I have implemented onTouch as above in the code, these two methods does not get triggered.
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
Regarding Zoom effect on Click event.
In my Fragment I am zooming image like this.
offerimg1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
zoomAnimation(v, offerimg1);
}
});
And my zoomAnimation method is :
private void zoomAnimation(View view,ImageView image) {
Animation animation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.zoom);
image.startAnimation(animation);
}
My Zoom is working unless I have not implemented in my fragmented :
offerimg1.setOnTouchListener(new OnSwipeTouchListener(getActivity().getApplicationContext(),offerimg1.getX(),offerimg1.getY()) {
@Override
public void onSwipeLeft() {
Log.d("onTouch "," swipe left");
}
@Override
public void onSwipeRight() {
Log.d("onTouch "," swipe right");
}
});
I am not sure what is the collision between these events. I need to achieve my above use case, every events should work on each image. As my images are in a scroll view in a fragment.
If you want I can share more code here. Please let me know if I could not clarify my problem.
I really appreciate your help.
Regards, Shashank Pratap