how to make android flipper animation using onFling()? - java

I'm having trouble in adding a little animation in onfling method. Everything is working just fine. Flipping pages left to right and right to left is done but there's no animation. I tried various things but couldn't get the animation working. Please guide me how can I add flipper animation or swipe animation. Here is the code:
public class MainActivity extends CustomTitlebarActivityBase {
// some random variables..
// detect swipe left/right
private GestureDetector gestureDetector;
private MyGestureListener gestureListener;
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Creating MainActivity");
super.onCreate(savedInstanceState, true);
setContentView(R.layout.main_view);
// create related objects
gestureListener = new MyGestureListener(MainActivity.this);
gestureDetector = new GestureDetector( gestureListener );
documentViewManager = new DocumentViewManager(this);
documentViewManager.buildView();
myContentManager = new myContentManager(documentViewManager);
// force the screen to be populated
myContentManager.updateText(true);
}
// there are some other methods
/** user swiped left */
public void next() {
if (getDocumentViewManager().getDocumentView().isPageNextOkay()) {
CurrentPageManager.getInstance().getCurrentPage().next();
}
}
/** user swiped left */
public void previous() {
if (getDocumentViewManager().getDocumentView().isPagePreviousOkay()) {
CurrentPageManager.getInstance().getCurrentPage().previous();
}
}
}
// here is the listener class
public class MyGestureListener extends SimpleOnGestureListener {
// measurements in dips for density independence
private static final int DISTANCE_DIP = 40;
private int scaledDistance;
private int minScaledVelocity;
private MainActivity mainActivity;
private boolean sensePageDownTap;
private static final String TAG = "MyGestureListener";
public MyGestureListener(MainActivity mainActivity) {
super();
this.mainActivity = mainActivity;
scaledDistance = CommonUtils.convertDipsToPx(DISTANCE_DIP);
minScaledVelocity = ViewConfiguration.get(mainActivity).getScaledMinimumFlingVelocity();
// make it easier to swipe
minScaledVelocity = (int)(minScaledVelocity*0.66);
}
#Override
public void onLongPress(MotionEvent e) {
// do something
}
// here is the onFling
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// get distance between points of the fling
double vertical = Math.abs( e1.getY() - e2.getY() );
double horizontal = Math.abs( e1.getX() - e2.getX() );
Log.d(TAG, "onFling vertical:"+vertical+" horizontal:"+horizontal+" VelocityX"+velocityX);
if ( vertical > scaledDistance ) {
return false;
}
// test horizontal distance and velocity
else if ( horizontal > scaledDistance && Math.abs(velocityX) > minScaledVelocity ) {
if (e1.getX() > e2.getX()) {
mainActivity.next();
}
// left to right swipe
else {
mainActivity.previous();
}
return true;
}
return false;
}
}

Try to implement OnGestureListener. Sample code is beloe
public class MyActivity extends Activity implements GestureDetector.OnGestureListener {
private GestureDetector gestureScanner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
this.gestureScanner = new GestureDetector(this);
}
#Override
public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
super.dispatchTouchEvent(paramMotionEvent);
return this.gestureScanner.onTouchEvent(paramMotionEvent);
}
public boolean onDown(MotionEvent paramMotionEvent) {
return false;
}
public boolean onFling(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1,
float paramFloat2) {
// your fling code goes here
return true;
}
public void onLongPress(MotionEvent paramMotionEvent) {
}
public boolean onScroll(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1,
float paramFloat2) {
return false;
}
public void onShowPress(MotionEvent paramMotionEvent) {
}
public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
return false;
}
}

Related

How to Track all gestures on ENTIRE android screen?

How do I track all gestures on an entire application screen. I was struggling with adding a gesture detector to the entire screen of my application. I am using gesturedetector compat to get gestures from the layout however when I tap or double tap the button and edit text gesturedetector does not detect the gesture on the view. Is it possible to have all tap,swipes,gestures on the ENTIRE screen as opposed to only the layout. If there is a better solution to tracking all touches,swipes,taps please let me know I have struggling with this for weeks.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private GestureDetectorCompat mGestureDetector;
private TextView touchCheckText;
private TextView singleTapText;
private TextView doubleTapText;
private static final String TAG = "Gesture ";
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN)
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetectorCompat(this,new GestureListener());
touchCheckText = (TextView) findViewById(R.id.touchCheckTextView);
scrollText = (TextView) findViewById(R.id.scrollTextView);
singleTapText = (TextView) findViewById(R.id.singleTapTextView);
doubleTapText = (TextView) findViewById(R.id.doubleTapTextView);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener{
#Override
public boolean onDoubleTap(MotionEvent e) {
int x = (int) e.getX();
int y = (int) e.getY();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
Log.e(TAG, "in on onDoubleTap Event");
doubleTapText.setBackgroundColor(Color.GREEN);
return super.onDoubleTap(e);
}
#Override
public boolean onDown(MotionEvent e) {
touchCheckText.setText("TOUCHING!");
return super.onDown(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
int x = (int) e.getX();
int y = (int) e.getY();
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
singleTapText.setBackgroundColor(Color.GREEN);
Log.e(TAG, "Single Tap "+ts+" x:"+x+" y:"+y);
singleTapText.postDelayed(new Runnable() {
#Override
public void run() {
// change color in here
singleTapText.setBackgroundColor(Color.TRANSPARENT);
}
}, 100);
return super.onSingleTapConfirmed(e);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
switch ( event.getAction() ) {
case MotionEvent.ACTION_UP:
Log.e("onTouch","Released");
doubleTapText.setBackgroundColor(Color.TRANSPARENT);
touchCheckText.setText("No Longer Touching");
break;
}
return super.onTouchEvent(event);
}
}

I need to access a library command in a different method in my code

Okay so I'm going to try to make this as simple as possible as I just keep getting confused thinking about it. So I'm using a library called AchievementUnlocked which you can find here Github Link
So yeah I'll post the code below but basically I need to access the method below from another method in my activity.
Method I need to access:
public void notification() {
int seconds = Integer.parseInt(duration);
notificationstatus = "ACTIVE";
final AchievementUnlocked achievementUnlocked = new AchievementUnlocked(MainActivity.this).setTitle(title).
setSubtitleColor(Color.parseColor(colortxt)).
setSubTitle(text).
setDuration(Settings.Time).
setBackgroundColor(Color.parseColor(colorbg)).
setTitleColor(Color.parseColor(colortxt)).
setIcon(getDrawableFromRes(image1)).
isLarge(Settings.largepref).build();
View iconIV = achievementUnlocked.getIconView();
ObjectAnimator outX = ObjectAnimator.ofFloat(iconIV, "scaleX", 0.9f, 0.7f);
ObjectAnimator outY = ObjectAnimator.ofFloat(iconIV, "scaleY", 0.9f, 0.7f);
ObjectAnimator inX = ObjectAnimator.ofFloat(iconIV, "scaleX", 0.7f, 0.9f);
ObjectAnimator inY = ObjectAnimator.ofFloat(iconIV, "scaleY", 0.7f, 0.9f);
final AnimatorSet Outset = new AnimatorSet();
final AnimatorSet Ani = new AnimatorSet();
final AnimatorSet Inset = new AnimatorSet();
outX.setDuration(1000);
outY.setDuration(1000);
inX.setDuration(1000);
inY.setDuration(1000);
Ani.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Ani.start();
}
});
Outset.playTogether(outX, outY);
Inset.playTogether(inX, inY);
(achievementUnlocked.getAchievementView()).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
achievementUnlocked.dismiss();
NotificationService.run();
}
});
achievementUnlocked.getAchievementView().setOnTouchListener(gestureListener);
Ani.play(Outset).before(Inset);
achievementUnlocked.setAchievementListener(new AchievementUnlocked.achievementListener() {
#Override
public void onAchievementBeingCreated(AchievementUnlocked achievement, boolean created) {
}
#Override
public void onAchievementExpanding(AchievementUnlocked achievement, boolean expanded) {
if (expanded) Ani.start();
}
#Override
public void onAchievementShrinking(AchievementUnlocked achievement, boolean shrunken) {
if (!shrunken) {
if (Ani.isRunning())
Ani.cancel();
notificationstatus = "UNACTIVE";
Log.i("Status Set UNACTIVE", notificationstatus);
}
}
#Override
public void onAchievementBeingDestroyed(AchievementUnlocked achievement, boolean destroyed) {
}
});
achievementUnlocked.show();
}
I need to access that method from this section of code:
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// left to right swipe
if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
**Rightswipe();**
}
// right to left swipe
else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
**Leftswipe();**
}
} catch (Exception e) {
// nothing
}
return false;
}
}
So my main question is how can I act like I'm inside the original method from my second method because the function achievementUnlocked.dismiss(); is not accessible outside of the main method you see first . I really appreciate any help!
Add a setter to MyGestureDetector that takes an AchievementUnlocked as a parameter. Store that AchievementUnlocked in a field in MyGestureDetector. Call that setter from notification(). Then, MyGestureDetector has access to the AchievementUnlocked instance and can call dismiss() on it.

Implementing GestureDetector to Drag, DoubleTap and singleTap a ImageView

I am trying to implement a ImageView which will have different reaction with each action(Drag, DoubleTap and Single Tap). What I am doing is based in this tutorial:
DoubleTap in android
Which I used to create my class:
public class MyIcon extends ImageView {
GestureDetector gestureDetector;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// creating new gesture detector
gestureDetector = new GestureDetector(context, new GestureListener());
}
// skipping measure calculation and drawing
// delegate the event to the gesture detector
#Override
public boolean onTouchEvent(MotionEvent e) {
return gestureDetector.onTouchEvent(e);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
#Override
public boolean onDoubleTap(MotionEvent e) {
//A Toast just to see if it is working
return true;
}
#Override
public boolean onShowPress(MotionEvent e) {
//A Toast just to see if it is working
return true;
}
}
}
To instantiate:
private WindowManager windowManager;
private MyIcon myIcon;
private WindowManager.LayoutParams params;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new MyIcon(this);
chatHead.setImageResource(R.mipmap.ic_launcher);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
I used a setOnTouchListener() which worked fine to make do Drag, but it is not work to DoubleTap.
Which would be the correct way to implement and instantiate this class to both work?
try this may help you SimpleGestureFilter
public class SimpleGestureFilter extends SimpleOnGestureListener {
public final static int SWIPE_UP = 1;
public final static int SWIPE_DOWN = 2;
public final static int SWIPE_LEFT = 3;
public final static int SWIPE_RIGHT = 4;
public final static int MODE_TRANSPARENT = 0;
public final static int MODE_SOLID = 1;
public final static int MODE_DYNAMIC = 2;
private final static int ACTION_FAKE = -13; // just an unlikely number
private int swipe_Min_Distance = 100;
private int swipe_Max_Distance = 350;
private int swipe_Min_Velocity = 100;
private int mode = MODE_DYNAMIC;
private boolean running = true;
private boolean tapIndicator = false;
private Activity context;
private GestureDetector detector;
private SimpleGestureListener listener;
public SimpleGestureFilter(Activity context, SimpleGestureListener sgl) {
this.context = context;
this.detector = new GestureDetector(context, this);
this.listener = sgl;
}
public void onTouchEvent(MotionEvent event) {
if (!this.running)
return;
boolean result = this.detector.onTouchEvent(event);
if (this.mode == MODE_SOLID)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.mode == MODE_DYNAMIC) {
if (event.getAction() == ACTION_FAKE)
event.setAction(MotionEvent.ACTION_UP);
else if (result)
event.setAction(MotionEvent.ACTION_CANCEL);
else if (this.tapIndicator) {
event.setAction(MotionEvent.ACTION_DOWN);
this.tapIndicator = false;
}
}
// else just do nothing, it's Transparent
}
public void setMode(int m) {
this.mode = m;
}
public int getMode() {
return this.mode;
}
public void setEnabled(boolean status) {
this.running = status;
}
public void setSwipeMaxDistance(int distance) {
this.swipe_Max_Distance = distance;
}
public void setSwipeMinDistance(int distance) {
this.swipe_Min_Distance = distance;
}
public void setSwipeMinVelocity(int distance) {
this.swipe_Min_Velocity = distance;
}
public int getSwipeMaxDistance() {
return this.swipe_Max_Distance;
}
public int getSwipeMinDistance() {
return this.swipe_Min_Distance;
}
public int getSwipeMinVelocity() {
return this.swipe_Min_Velocity;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final float xDistance = Math.abs(e1.getX() - e2.getX());
final float yDistance = Math.abs(e1.getY() - e2.getY());
if (xDistance > this.swipe_Max_Distance
|| yDistance > this.swipe_Max_Distance)
return false;
velocityX = Math.abs(velocityX);
velocityY = Math.abs(velocityY);
boolean result = false;
if (velocityX > this.swipe_Min_Velocity
&& xDistance > this.swipe_Min_Distance) {
if (e1.getX() > e2.getX()) // right to left
this.listener.onSwipe(SWIPE_LEFT);
else
this.listener.onSwipe(SWIPE_RIGHT);
result = true;
} else if (velocityY > this.swipe_Min_Velocity
&& yDistance > this.swipe_Min_Distance) {
if (e1.getY() > e2.getY()) // bottom to up
this.listener.onSwipe(SWIPE_UP);
else
this.listener.onSwipe(SWIPE_DOWN);
result = true;
}
return result;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
this.tapIndicator = true;
return false;
}
#Override
public boolean onDoubleTap(MotionEvent arg) {
this.listener.onDoubleTap();
;
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent arg) {
return true;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent arg) {
if (this.mode == MODE_DYNAMIC) { // we owe an ACTION_UP, so we fake an
arg.setAction(ACTION_FAKE); // action which will be converted to an
// ACTION_UP later.
this.context.dispatchTouchEvent(arg);
}
return false;
}
static interface SimpleGestureListener {
void onSwipe(int direction);
void onDoubleTap();
}
}
Use in code this way
private SimpleGestureFilter detector;
detector = new SimpleGestureFilter(this, this);
Just change in this method
#Override
public boolean dispatchTouchEvent(MotionEvent me) {
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}

How to receive events from OnGestureListener

I created a class that implements GestureDetector.OnGestureListener. However, I do not know how these methods get called. Is there something like view.setOnTouchListener() for a GestureListener that allows you to receive the events? Thank you in advanced. Here is my class
public class GestureHandler implements GestureDetector.OnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int direction = -1;
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
//Left Swipe
if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 270;
Log.d("Swiped: ", direction + "");
return true;
//Right Swipe
} else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
direction = 90;
Log.d("Swiped: ", direction + "");
return true;
}
//Up Swipe
if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 0;
Log.d("Swiped: ", direction + "");
return true;
//Down Swipe
} else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE &&
Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
direction = 180;
Log.d("Swiped: ", direction + "");
return true;
}
direction = -1;
Log.d("Swiped: ", direction + "");
return false;
}
}
Very simple solution here. :)
Here is how I did this in my project, working with Google Glass -
I didn't actually extend GestureDetector. I had something like this:
public class EXAMPLE {
private GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
});
return gestureDetectorTemp;
}
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
}
That last part is very important. On any generic motion event, if the gestureDetector isn't null, you'll send the event through the gestureDetector for processing.
KEEP IN MIND ALSO that you need to understand what the return false; and return true; things mean. If you return false, then that means that the event wasn't consumed. If you return true, then the event is consumed. In other words, if you return true, then nothing else will activate, because the event gets 'eaten up,' but if you return false, this sends the event on to other functions which may do something when an action is taken.
I hope this helps. Good luck :)

How to swipe zoomed image using ImageViews and PageAdapter

I am trying to create an image viewer which can load images from given URLs.The code below implement the User Interface.
My intention is to let user Zoom the image and move to next image with a Swipe event.But the problem is that when i zoom and then swipe , instead of showing the remaining portion , it moves to the next image.
I tried using requestDisallowInterceptTouchEvent in TouchImageView's(https://github.com/MikeOrtiz/TouchImageView) onTouchListener .After this the remaining portion could show but now i cannot go to the next page. I was wondering how this can be achieved as the event can only go to either TouchView or PageAdapter
public class PageActivity extends Activity {
private int numPages = 33;
private TouchImageView[] imageViews = new TouchImageView[numPages];
private String URL = "http://www.smbc-comics.com/comics/200905";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewPager viewPager = new ViewPager(this);
for (int i = 0; i < numPages; i++) {
imageViews[i] = new TouchImageView(this);
imageViews[i].setBackgroundResource(R.drawable.banke);
imageViews[i].setMaxZoom(4f);
}
setContentView(viewPager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(2);
}
#SuppressWarnings("unused")
private class ImagePagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return numPages;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((TouchImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = PageActivity.this;
String pageURL = URL;
if (imageViews[position].getDrawable() == null) {
ImageFetcher imagefetcher = new ImageFetcher();
imagefetcher.execute(
pageURL + String.format("%02d", position+1) + ".gif",
String.valueOf(position));
}
((ViewPager) container).addView(imageViews[position], 0);
return imageViews[position];
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((TouchImageView) object);
imageViews[position].setImageDrawable(null);
}
}
public class ImageFetcher extends AsyncTask<String, Integer, Drawable> {
int fillthisPos;
public Drawable doInBackground(String... urls) {
try {
InputStream is = (InputStream) new URL(urls[0]).getContent();
fillthisPos = Integer.parseInt(urls[1]);
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
#Override
protected void onPostExecute(Drawable result) {
super.onPostExecute(result);
imageViews[fillthisPos].setImageDrawable(result);
result = null;
}
}
}
You can add following code in TouchImageView class:
public boolean isZoomed () {
return (normalizedScale > minScale);
}
private void onSwipeEvent(MotionEvent event) {
boolean zoomed = this.isZoomed();
if (!zoomed && (swipeLen > 0)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
swipeStartPos = (int) event.getRawX();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int distance = ((int) event.getRawX()) - swipeStartPos;
int swipeVector = SWIPE_RIGHT;
if (distance < 0) swipeVector = SWIPE_LEFT;
if (Math.abs(distance) > swipeLen) {
onSwipeHandler.onSwipe(swipeVector);
swipeStartPos = (int) event.getRawX();
this.setScaleX(1f);
}
else {
int swipeStDist = swipeLen - Math.round(((swipeLen / 100) * 50));
if (Math.abs(distance) > swipeStDist) {
this.setScaleX(0.98f);
}
}
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
swipeStartPos = (int) event.getRawX();
this.setScaleX(1f);
}
}
}
public static int SWIPE_LEFT = 0;
public static int SWIPE_RIGHT = 1;
public int swipeStartPos = 0;
public onSwipeListener onSwipeHandler = new onSwipeListener() {
public void onSwipe(int vector) {}
};
public static int swipeLen = 0;
public void setOnSwipeListener(onSwipeListener c, int swipeLength) {
onSwipeHandler = c;
swipeLen = swipeLength;
}
public interface onSwipeListener {
public void onSwipe(int vector);
}
And also in TouchImageView below line 636 add onSwipeEvent(event) like this:
......
setImageMatrix(matrix);
onSwipeEvent(event);
//
// indicate event was handled
//
return true;
...........
After this from you code you can add swipe event listener, like this:
imageView.setOnSwipeListener(new TouchImageView.onSwipeListener() {
#Override
public void onSwipe(int vector) {
if (vector == TouchImageView.SWIPE_LEFT) {
Log.d("swipe", "swipe left!");
}
else if (vector == TouchImageView.SWIPE_RIGHT) {
Log.d("swipe", "swipe right!");
}
}
}, 200); //length of swiping - 200 dip
This onSwipeListener ignore onswipe where image is zoomed+. It work for me.

Categories