9

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?

bdls
  • 4,578
  • 6
  • 24
  • 30

5 Answers5

18

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.

Community
  • 1
  • 1
abudker
  • 612
  • 2
  • 9
  • 22
  • 2
    Doesn't seems to work on sub-class Gallery, any idea who this is the case? – Teo Choong Ping Jan 26 '11 at 05:38
  • Doesn't seem to work at all on my HTC Desire. Just using it as a normal Gallery, triggered by a button press – Kurru Feb 23 '11 at 01:18
  • Does this code work? I can't make it move. Do I miss something? And also, how can I make it move to a certain position, using it as many times as my pos in a "for"? – AnTz Mar 21 '13 at 21:51
3

You can Animate using dispatchKeyEvent or calling onFling directly.

Here is sample code for dispatchKeyEvent:

KeyEvent evtKey = new KeyEvent(0, KeyEvent.KEYCODE_DPAD_RIGHT);
dispatchKeyEvent(evtKey);
BlueDares
  • 31
  • 1
2

Use gallery.setSelected(int); Here is a simple example.


public class Splash extends Activity {

    ArrayList objects = new ArrayList();
    Gallery g;
    int i = 0;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.photos);
        g = (Gallery) findViewById(R.id.gallery);
        objects.add(getResources().getDrawable(R.drawable.icon));
        objects.add(getResources().getDrawable(R.drawable.icon));
        objects.add(getResources().getDrawable(R.drawable.icon));
        objects.add(getResources().getDrawable(R.drawable.icon));
        objects.add(getResources().getDrawable(R.drawable.icon));
        objects.add(getResources().getDrawable(R.drawable.icon));
        g.setAdapter(new CustomAdapter(this, objects));
        g.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView arg0, View arg1,
                    int arg2, long arg3) {
                Log.i("", "selected " + arg2);
            }

            @Override
            public void onNothingSelected(AdapterView arg0) {}
        });
    }

    @Override
    public void onBackPressed() {
            g.setSelection(i++);
    }

        private class CustomAdapter extends BaseAdapter {

        private Context mCtx;
        private List objects;

        public int getCount() {
            return this.objects.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public CustomAdapter(Context context, ArrayList objects) {
            super();
            mCtx = context;
            this.objects = objects;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView row = (ImageView) convertView;
            if (row == null) {
                row = new ImageView(mCtx);
                row.setBackgroundDrawable(objects.get(position));
            }
            return row;
        }
    }
}
Dave.B
  • 6,632
  • 1
  • 19
  • 20
  • Hi - Thank you for your response, however your solution doesn't appear to provide animation when changing between selected images. Animation is a requirement. – bdls Apr 28 '10 at 22:39
1

In the end I wrote my own version of the Gallery widget with the help of the code at this site.

I then wrote my own method which uses mFlingRunnable.startUsingDistance(distance);

Now I can programmatically animate the gallery between images.

bdls
  • 4,578
  • 6
  • 24
  • 30
1

Try this

mGallery.onFling(null,null, velocity,0);

http://groups.google.com/group/android-developers/browse_thread/thread/9140fd6af3061cdf#

Akos Cz
  • 12,711
  • 1
  • 37
  • 32