How can I determine motion direction in an Android Gallery? - android

I have been creating a Gallery derivative that uses a limited number of Views and, as such, the Adapter needs to be able to populate these Views ahead of time during a scroll or a fling. To do this, I need to get the direction of motion from the onFling(...) and onScroll(...) events.
How can I use the distanceX parameter in onScroll(...) and the velocityX parameter in onFling(...) to determine which way the Gallery is travelling, and therefore which View to prepare next?

The signs of the velocity parameters in onFling(...) and the distance parameters on onScroll(...) are opposite. In order to correctly determine which direction a Gallery is moving, the code should be as follows:
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//if distanceX is POSITIVE, the Views are travelling left
//therefore the selection position is INCREASING
return super.onScroll(e1, e2, distanceX*mVelocityFactor, distanceY*mVelocityFactor);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//if velocityX is NEGATIVE, the Views are travelling left
//therefore the selection position is DECREASING
return super.onFling(e1, e1, velocityX*mVelocityFactor, velocityY*mVelocityFactor);
}
By the way, mVelocityFactor is just a constant I introduced to make the scroll/fling a little less energetic. I found 0.6 to be quite a nice value—it still feels intuitive to scroll but the flings are less violent.

Related

Gallery swipe adjustment

I need to lessen the amount of distance to be swiped in order to change my gallery's picture to the next. Right now this distance is at the standard rate and it's about half of the width of the gallery, but I need it to be about 1/3 or 1/4 of the total gallery width.
Thanks.
UPD: right now my custom gallery onFling returns false in order to list per page:
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}

Android Only one item change per fling in gallery

I had implemented a custom Gallery with custom Adapter.
My Requirement is that however fast or slow does the user operates fling on the gallery only one item should change in the gallery.
I tried to override the Gallery's onFling method
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//Log.e("VELOCITY ORIGINAL", ""+velocityX);
if (velocityX > 500) {//moving left
velocityX = 500.0f;
}else if(velocityX < -500){//moving right
velocityX = -500.0f;
}
//Log.e("VELOCITY MODIFIED", ""+velocityX);
return super.onFling(e1, e2, velocityX, velocityY);
}
But this did not produce desired results as sometimes it would flick one item and some times it won't.
Then I tried
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
But this completely disabled fling operation.
What could be the solution?
For that you have to override the onFling method in the Gallery class Try this and just Use ExtendedGallery instead of Gallery

Android Gallery Full width, no gap

I need to build something like a stream for my android app (like pulse)
I tried several horizontalListview or HorizontalScrollView but it's not smooth at all!
The smoother widget I found is the gallery.
I successfully tweaked it to right align:
http://cl.ly/0a3Q002u3H1f3w2l0g2e
My problem is when you scroll max to the right. It looks like stream #3.
Is there a way change this to avoid the gap on the right ?
Maybe to changing the maximum selectable position (# of elements - 4 in my case)
Thank you.
Actually I use this one https://github.com/vieux/Android-Horizontal-ListView
I found the problem about smoothness. The vertical listview shouldn't react when we are already scrolling a stream horizontally.
You could try something like
public class GalleryChild extends Gallery {
...
#Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if (getSelectedItemPosition() >= getChildCount() - BUFFER) {
setSelection(getChildCount() - BUFFER, true);
/** Eat the event. */
return true;
} else return super.onScroll(e1, e2, distanceX, distanceY);
}

How to detect scrolling action in Android?

I have a horizontal scroll view, and I want to detect a scrolling action ,
1) is there a listener to detect when the user make scrolling ?
2) and can I know whether the scroll is left to right or right to left ?
EDIT :what is the required listener in HorizontalScrollView ? I don't find OnScrollListener !
You need to go for Gestures. You will have callback methods like
onScroll(MotionEvent e1, MotionEvent
e2, float distanceX,float distanceY)
onFling(MotionEvent e1, MotionEvent
e2, float velocityX,float velocityY)

Android: Measure/detect covered area by a finger touch on screen (NOT only touch coordinates)

I would like to get access to the area covered by a finger for each touch event on an Android.
Every touch event will result in a coordinate pair X and Y independent of how big the finger and consequently the touch area is that triggered the event.
I was wondering if there is a way to get the area data which triggered the touch event e.g. size or coordinates NOT
Any help is much appreciated.
Thanks in advance for you answer or redirects,
Christian
The method motionEvent.getSize() should give you what you want (but the level of accuracy may vary depending on the device's screen).
You need to implement OnGestureListener.
First of all, you need to register GestureDetector in onTouchEvent
#Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
}
In onShowPress you will get starting points
#Override
public void onShowPress(MotionEvent e) {
startX = e.getX();
startY = e.getY();
}
In onScroll you will get the end points.
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
endX = e2.getX();
endY = e2.getY();
}

Categories

Resources