如何在Android中制作连续运行的TransitionDrawable动画?
我正在尝试制作一个动画徽标。它由两个静态图像组成。
我想实现交叉淡入淡出效果。
我已经使用 TransitionDrawable 完成了它,设置了 crossFadeEnabled,一切看起来都不错。
问题是我需要绕圈跑。如何才能实现呢?
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_expand">
<item android:drawable="@drawable/image_collapse">
</transition>
Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);
这是来自谷歌的代码,运行完美。 最重要的是需要在Android 1.6下工作。
I'm trying to make an animated logo. It consists of two static images.
I would to like to achieve a cross-fading effect.
I've done it with the use of TransitionDrawable, set the crossFadeEnabled and everything looks nice.
The thing is that I need to be running in circle. How can it be achieved ?
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_expand">
<item android:drawable="@drawable/image_collapse">
</transition>
Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);
This the code from google which runs perfectly.
The most importantn thing is that in needs to work under Android 1.6.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法通过处理程序方法使转换可绘制对象正常工作,该方法在从可绘制对象 1 转换为可绘制对象 2 后反转方向。在我的 XML 中:
可绘制对象是渐变的:
在我的初始化代码中:
处理程序代码中最重要的部分;请注意全局运行的标志。我在 Gingerbread 和 Kitkat 上运行了这个;
I managed to get the transition drawable to work via a handler method that reverses direction after the transition from drawable1 to drawable2. In my XML's :
The drawables are gradients :
In my initialisation code :
And the most important bit in the handler code; note the flag that is running globally. I got this running on Gingerbread and Kitkat;
并没有真正的内置方法可以做到这一点。
TransitionDrawable
没有设计无限循环动画功能。最简单的建议是在仅包含一个Drawable
的View
上使用Animation
(alpha、scale、translate 等)如果可以的话。一个相当简单的技巧是向保存
TransitionDrawable
的自定义View
添加一个Handler
和回调。创建View
时,可以将Handler
设置为您的转换间隔。View
还将实现Handler.Callback
并在其handleMessage(Message)
方法内调用reverseTransition(int)
TransitionDrawable
上的 code>。一个粗略的示例如下:
调用
start()
开始连续转换,调用stop()
停止它。这有点像创建自己的动画,但它可以在紧要关头发挥作用。There is not really a built-in way to do this.
TransitionDrawable
is not designed with an infinite looping animation function. The easiest recommendation would be to use anAnimation
(alpha, scale, translate, etc.) on aView
containing just one of yourDrawable
s if you can.A fairly easy hack would be to add a
Handler
and callback to your customView
holding yourTransitionDrawable
. When theView
is created, theHandler
can be set to your transition interval. TheView
would also implementHandler.Callback
and inside itshandleMessage(Message)
method, it would callreverseTransition(int)
on yourTransitionDrawable
.A rough example is below:
Call
start()
to start your continuous transition,stop()
to stop it. It is a bit like creating your own animation, but it works in a pinch.