Android: Programmatically animate between images in Gallery widget

亡梦爱人 提交于 2019-11-28 20:53:16
abudker

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!