Android:以编程方式在图库小部件中的图像之间设置动画
注意:从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需直接调用画廊的按键处理程序即可:
即
一件重要的事情 - 仅当已创建左侧/右侧的子项时此解决方案才有效,这意味着它必须是“可见的”。如果您的图像全屏显示 - 考虑将间距设置为 -1 值。
Just call the key press handler for the gallery directly:
i.e
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.
使用 gallery.setSelected(int);这是一个简单的例子。
Use gallery.setSelected(int); Here is a simple example.
您可以使用dispatchKeyEvent或直接调用onFling来制作动画。
这是dispatchKeyEvent的示例代码:
You can Animate using dispatchKeyEvent or calling onFling directly.
Here is sample code for dispatchKeyEvent:
最后,我借助 此网站。
然后,我编写了自己的方法,该方法使用 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.
试试这个
mGallery.onFling(null,null,velocity,0);
http://groups.google.com/group/android-developers/ browser_thread/thread/9140fd6af3061cdf#
Try this
mGallery.onFling(null,null, velocity,0);
http://groups.google.com/group/android-developers/browse_thread/thread/9140fd6af3061cdf#