Android:以编辑方式在Gallery小部件中的图像之间设置动画 [英] Android: Programmatically animate between images in Gallery widget

查看:205
本文介绍了Android:以编辑方式在Gallery小部件中的图像之间设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:从Jellybean开始,不推荐使用gallery小部件。应该使用 ViewPager

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

我想以编程方式在Gallery小部件中的图像之间移动动画。

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

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

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.

图库来源它似乎可以处理DPAD按键,所以我想到的解决办法是伪造按键。例如。

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?

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

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?

推荐答案

只需直接调用图库的按键处理程序:

Just call the key press handler for the gallery directly:

public boolean onKeyDown(int keyCode, KeyEvent event)

ie

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

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

一个重要的事情 - 这个解决方案只有在左/右的孩子已经创建时才有效,意味着它必须是可见的。如果您的图像是全屏的 - 请考虑将间距设置为-1值。

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.

这篇关于Android:以编辑方式在Gallery小部件中的图像之间设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆