How to receive events from OnGestureListener - java

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 :)

Related

How can i delete an item inside a nested recycler view using ItemTouchHelper?

I'm actually using Groupie library, and I'm trying to delete a nested item inside a group adapter.
But when I use ItemTouchHelper, it only deletes the parent items(Objetos) or the nested items(Produtos), basically the first one that I try to swipe. It attaches just one of the ItemTouchHelpers to my RecyclerView.
Here is my Fragment Code:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
metodos = new OSMetodos(requireContext(), requireActivity(), codOS);
adapter = new GroupAdapter<>();
adcTipoServ = view.findViewById(R.id.adcTipoServico);
adcTipoServ.setOnClickListener(view1 -> metodos.addService());
recyclerViewOS = view.findViewById(R.id.recyclerViewOSServ);
recyclerViewOS.setLayoutManager(new LinearLayoutManager(requireContext()));
setAdapter();
}
#Override
public void onResume() {
super.onResume();
setAdapter();
}
public void setAdapter() {
oldList.clear();
adapter.clear();
oldList = repo.getObjetoTipoServicoServicoCompleteWrappers(codOS, "D");
for (ObjetoTipoServicoServicoCompleteWrapper wrapper : oldList) {
servico = new OSServicoItem(wrapper, requireContext(), this);
objGroup = new ExpandableGroup(servico, false);
produtos = new ArrayList\<\>();
garantias = new ArrayList\<\>();
for (ProdutoServicoWithComodoProduto prod : wrapper.produtoServicoWithComodoProdutos) {
produtoItem = new OSServProdutoItem(prod, requireContext(), this);
produtoDetalhes = new OSServProdutoDetalhes(prod, requireContext(), requireActivity(), codOS);
prodGroup = new ExpandableGroup(produtoItem, false);
prodGroup.add(produtoDetalhes);
produtos.add(prodGroup);
}
for (GarantiaServicoClienteNewEntity garan : wrapper.garantiaServicoClienteNewEntities) {
garantiaItem = new OSServGarantiaItem(garan, requireContext());
garantias.add(garantiaItem);
}
prodSection = new Section(new OSServProdBtn(this), produtos);
garanSection = new Section(new OSServGaranBtn(this), garantias);
objGroup.add(prodSection);
objGroup.add(garanSection);
adapter.add(objGroup);
}
recyclerViewOS.setAdapter(adapter);
}
#Override
public void onSwiped(#Nullable ObjetoTipoServicoServicoCompleteWrapper objeto, #Nullable ProdutoServicoWithComodoProduto wrapper, int op) {
if (op == 0) {
SwipeToDelete swipeToDelete = new SwipeToDelete(0, ItemTouchHelper.LEFT, requireContext(), adapter, wrapper, null, oldList, requireActivity(), repo, op);
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeToDelete);
itemTouchhelper.attachToRecyclerView(recyclerViewOS);
} else {
SwipeToDelete swipeToDelete = new SwipeToDelete(0, ItemTouchHelper.LEFT, requireContext(), adapter, null, objeto, oldList, requireActivity(), repo, op);
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeToDelete);
itemTouchhelper.attachToRecyclerView(recyclerViewOS);
}
}
Here's my ParentItem GestureHelper:
public class GestureHelper implements View.OnTouchListener {
private final GestureDetector mGestureDetector;
private ObjetoTipoServicoServicoCompleteWrapper objeto;
public GestureHelper(Context context, ObjetoTipoServicoServicoCompleteWrapper objeto) {
mGestureDetector = new GestureDetector(context, new GestureListener(this));
this.objeto = objeto;
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
onSwipe.onSwiped(objeto, null, 1);
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onDoubleTap() {
}
public void onClick() {
expandableGroup.onToggleExpanded();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureHelper mHelper;
public GestureListener(GestureHelper helper) {
mHelper = helper;
}
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
mHelper.onClick();
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
mHelper.onDoubleTap();
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
mHelper.onSwipeRight();
} else {
mHelper.onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
mHelper.onSwipeBottom();
} else {
mHelper.onSwipeTop();
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
}
And finally my NestedItem GestureHelper:
public class GestureHelper implements View.OnTouchListener {
private final GestureDetector mGestureDetector;
private ProdutoServicoWithComodoProduto wrapper;
public GestureHelper(ProdutoServicoWithComodoProduto wrapper) {
mGestureDetector = new GestureDetector(context, new GestureListener(this));
this.wrapper = wrapper;
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
onSwipeProduto.onSwiped(null, wrapper, 0);
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
public void onDoubleTap() {
}
public void onClick() {
expandableGroup.onToggleExpanded();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureHelper mHelper;
public GestureListener(GestureHelper helper) {
mHelper = helper;
}
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
mHelper.onClick();
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
mHelper.onDoubleTap();
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
mHelper.onSwipeRight();
} else {
mHelper.onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
mHelper.onSwipeBottom();
} else {
mHelper.onSwipeTop();
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
}

Android Textview slow and even doesn't work at all

I made a very simple app which displays current date in day of month inside a Textview at 120dp text size. I want to increment and decrement the date by one day every swipe right or swipe left gesture.
My code worked but it worked very slowly especially the swipe left gesture to decrement the date. Sometimes it refused to work.
Here is my code:
Calendar c;
Int day,month,year;
public abstract class OnSwipeTouchListener implements View.OnTouchListener {
public final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_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) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
result = true;
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
textView.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeTop() {}
public void onSwipeRight() {
c.add(Calendar.DATE, 1);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) +1;
year = c.get(Calendar.YEAR);
textView.setText(Integer.toString(day));
}
public void onSwipeLeft() {
c.add(Calendar.DATE, -1);
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH) +1;
year = c.get(Calendar.YEAR);
textView.setText(Integer.toString(day));
}
public void onSwipeBottom() {
textView.setText("DOWN");
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
Why is the performance of this code so poor?
Use RecyclerView with horizontal orientation instead TextView ...may be it will give much smooth & great performance then applying Gesture tracker on TextView..
Thank you guys. I managed to solve this issue myself, just set listener to the layout and everything works like a charm.

I Need to Implement Volume change Functionality + Screen Brightness Adjustment On Swipe/Movement on Screen Touch

How do i implement the swipe/touch volume and brightness adjustment functionality in the app.
I have Tried Playerview set on Touch Listener. ExoPlayer Set On Touch Listners. It On Touch Listeners Work Only if if Keep Swiping the View. I Need to implement Continuous swiping like the Functionalit in MX Player on Builtin Video Player App Of Any Android Mobile....
Volume Control
add permission android.permission.MODIFY_AUDIO_SETTINGS to your AndroidManifest.xml
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(
AudioManager.STREAM_MUSIC, // or STREAM_ACCESSIBILITY, STREAM_ALARM, STREAM DTMF, STREAM_NOTIFCATION, STREAM_RING, STREAM_SYSTEM, STREAM_VOICE_CALL
AudioManager.ADJUST_LOWER, // or ADJUST_RAISE, ADJUST_SAME
0 // or FLAG_PLAY_SOUND, FLAG_REMOVE_SOUND_AND_VIBRATE, FLAG_SHOW_UI, FLAG_VIBRATE, FLAG_ALLOW_RINGER_MODES
)
Adjusts the volume of a particular stream by one step in a direction.
This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.
This method has no effect if the device implements a fixed volume policy as indicated by isVolumeFixed().
From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed unless the app has been granted Do Not Disturb Access. See NotificationManager#isNotificationPolicyAccessGranted().
https://developer.android.com/reference/android/media/AudioManager
Brightness Control
add permission Manifest.permission.WRITE_SETTINGS to your AndroidManifest.xml
if (Settings.System.canWrite(getContext())) {
// values between 0-255
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 255); // full brightness
}
https://developer.android.com/reference/android/provider/Settings.System
Swiping:
credit to Mirek Rusin: Android: How to handle right to left swipe gestures
OnSwipeTouchListener.java:
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;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
#Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_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) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
}
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
Usage:
imageView.setOnTouchListener(new OnSwipeTouchListener(MyActivity.this) {
public void onSwipeTop() {
Toast.makeText(MyActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(MyActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(MyActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(MyActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
});

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 make android flipper animation using onFling()?

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;
}
}

Categories