Gallery move one picture - android

I use gallery to show several pictures.
When finger touch and move on screen, the picture to previous or next one.
But when the move distance large, the gallery may charge more than one picture.
I want to limit it move one picture every time move.
My code:
Gallery gallery = (Gallery) this.findViewById(R.id.gallery_photo);
gallery.setAdapter(new GalleryAdapter(this listPhotoURL));
gallery.setSelection(i);
listPhotoURL is string array;
In GalleryAdapter, only show listPhotoURL[i] to imageview.
How to arrive my goal?
Or modify listener method?

Below code might help you i.e you have to override this method of Gallery
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}

Related

How to move view using gesture detector and animation

I have added one view in linear layout and I want to move that view at run time in the direction user pushed it, for that I have added onTouchListner on view, as well as I have defined one gesture detector.
On gestureDetector's onFling method I have started translation animation on view
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
mX = e2.getX();
mY = e2.getY();
TranslateAnimation transA = new TranslateAnimation(
e1.getX(),e2.getX(),e1.getY(),e2.getY());
transA.setDuration(1000);
transA.setAnimationListener(MainActivity.this);
mBtn.startAnimation(transA);
return super.onFling(e1, e2, velocityX, velocityY);
}
translation animation works well but after animation view remains on its original position. To move view to destination location I am setting views x & y property to mX and mY
#Override
public void onAnimationEnd(Animation animation) {
mBtn.setX(mX);
mBtn.setY(mY);
}
but this is not working properly. please help, If there is any other way to achieve this please let me know.
Is there a button that is moving in there as well? I see a mBtn. Note that a view animation is only going to transform the place where a view is drawn and not where it actually is (hence a button clickable area won't move with a button). You could try doing transA.setFillAfter(true), that should stop it from going back to the original position, but I believe the clickable area will stay the same. Hence you should try doing a property animation, as it will update the properties of the view and not just where it is drawn. Doing a viewpropertyanimator might be easiest as you are animating x and y. You can read more here: http://developer.android.com/guide/topics/graphics/prop-animation.html

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

Android - How to prevent a user interacting with Gallery

OK, I know this questions sounds weird at first, as GALLERY is designed to be user interactive, but, I like the Gallery functionality, as it suits a lot of my needs. i.e. I can give it a number of pictures, move the whole of them from right to left, or left to right and get them an animate (in my case, zoom) when one of them is selected. So all good.
I just need to do the selection programatically, which I currently have working. I just don't want the user to be able to fling, scroll, select, longpress, etc. by themselves. So no user interaction is required.
So, how can I prevent a user from doing theses things, without writting a gallery function myself (and without chopping a users fingers off!).
Thanks.
Try setting the clickable and focusable properties of your Gallery View to false.
Following worked for me:
Gallery gallery = new Gallery( ctx ) {
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
If some interaction is still left you can also try:
Gallery dotList = new Gallery( ctx ) {
#Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return true;
}
};

How to best handle fling gesture for Android ListActivity

I have an Android app with a ListActivity in the main view. The list contains a LinearLayout with a TextView and a hidden delete Button. The delete Button will be hidden by default. I want to use a fling gesture to show the button. I am able to detect the fling gesture thanks to question #937313 on stackoverflow. It's not clear to me how to determine which item in the list was flung, since the onTouch listener listens to the ListView. The item is not necessarily selected so getSelected* methods can't be used reliably. I am using the SimpleListAdaptor so I don't have direct access to the View Objects in the ListView.
Any ideas?
Try using AbsListView.pointToPosition() to determine the list item for the X,Y coordinate in your list view.
If you're using the recipe from #937313, you should be able to override onFling() more or less as follows:
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
Toast.makeText(listAdapter.getItem( listView.pointToPosition( (int) e1.getX(), (int) e1.getY() ).toString() );
return super.onFling();
} catch( Exception e ) {
// do nothing
}
}

Categories

Resources