Android:以编程方式在图库小部件中的图像之间设置动画

发布于 2024-08-31 07:25:07 字数 838 浏览 2 评论 0原文

注意:从 Jellybean 开始,图库小部件已被弃用。应使用 ViewPager 代替。


我想以编程方式在图库小部件中的图像之间移动,并带有动画。

我可以使用 setSelection(intposition) 方法更改当前显示的图像,但这不会产生动画效果。然后是 setSelection(int position, bool animate) ,但最后的额外布尔值似乎没有做任何事情。

Gallery 的来源 看来它可以处理 DPAD 按键,所以我想到的解决方法是伪造按键。例如。

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

但是由于某种原因我无法让它工作。有人试过这个吗?

我注意到我喜欢使用的三个小部件方法 moveNext()movePrevious()scrollToChild() 都是私有且不可用的。

有谁知道我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人海汹涌 2024-09-07 07:25:07

只需直接调用画廊的按键处理程序即可:

public boolean onKeyDown(int keyCode, KeyEvent event)

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

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

一件重要的事情 - 仅当已创建左侧/右侧的子项时此解决方案才有效,这意味着它必须是“可见的”。如果您的图像全屏显示 - 考虑将间距设置为 -1 值。

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.

夜血缘 2024-09-07 07:25:07

使用 gallery.setSelected(int);这是一个简单的例子。


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

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;
        }
    }
}
顾冷 2024-09-07 07:25:07

您可以使用dispatchKeyEvent或直接调用onFling来制作动画。

这是dispatchKeyEvent的示例代码:

KeyEvent evtKey = new KeyEvent(0, KeyEvent.KEYCODE_DPAD_RIGHT);
dispatchKeyEvent(evtKey);

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);
厌倦 2024-09-07 07:25:07

最后,我借助 此网站

然后,我编写了自己的方法,该方法使用 mFlingRunnable.startUsingDistance(distance);

现在,我可以以编程方式在图像之间设置图库的动画效果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文