Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Programmatically animate between images in Gallery widget

Note: As of Jellybean the gallery widget is deprecated. A ViewPager should be used instead.


I'd like to programmatically move between images in the Gallery widget, with animation.

I can change the currently displaying image using the setSelection(int position) method, however that does not animate. Then there's setSelection(int position, bool animate) but the extra boolean on the end there doesn't appear to do anything.

In the source of Gallery it appears that it can handle DPAD key-presses, so a work-around I thought of was to fake the key-presses. Eg.

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT))

However I can't get this working for some reason. Anyone tried this?

I notice three of the widget's methods I'd love to use moveNext(), movePrevious() and scrollToChild() are all private and unusable.

Does anyone know how I might be able to do this?

like image 379
bdls Avatar asked Apr 28 '10 17:04

bdls


1 Answers

Just call the key press handler for the gallery directly:

public boolean onKeyDown(int keyCode, KeyEvent event)

i.e

Gallery gallery = ((Gallery) findViewById(R.id.gallery));

gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));

One important thing - this solution works only if child that is on left/right was already created, which means that it has to be 'visible'. If you have your image on fullscreen - consider setting spacing to -1 value.

like image 108
abudker Avatar answered Oct 21 '22 17:10

abudker