Android: Programmatically animate between images in Gallery widget(Android:以编程方式在图库小部件中的图像之间设置动画)
问题描述
注意:从 Jellybean 开始,画廊小部件已被弃用.应该使用 ViewPager.
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.
我可以使用 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)
即
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:以编程方式在图库小部件中的图像之间设置动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:以编程方式在图库小部件中的图像之间设置动画
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01