问题描述
我使用的翻译动画用于移动的ImageView
。我用这code:
I am using Translate Animation for moving an ImageView
. I am using this code:
TranslateAnimation set1 = new TranslateAnimation(-4, 10, -110, 0);
set1.setDuration(3000);
TranslateAnimation set2 = new TranslateAnimation(10, -3, 0, 115);
set2.setDuration(3000);
set2.setStartOffset(2200);
TranslateAnimation set3 = new TranslateAnimation(-3, -20, 0, -100);
set3.setDuration(3000);
set3.setStartOffset(4500);
TranslateAnimation set4 = new TranslateAnimation(0, 13, 0, -120);
set4.setDuration(3000);
set4.setStartOffset(6500);
animSet.addAnimation(set1);
animSet.addAnimation(set2);
animSet.addAnimation(set3);
animSet.addAnimation(set4);
animSet.setFillAfter(true);
创建一组动画后,我申请他们在的ImageView
是这样的:
image = (ImageView)findViewById(R.id.img);
image.startAnimation(animSet);
一切工作正常,但我不能暂停动画和恢复按钮点击
我怎么能这样做?
我什么都试过,但没有成功。任何想法如何做到这一点?
I tried everything, but didn't succeed. Any idea how to do this?
请帮忙!
推荐答案
搜索一段时间后,我发现这个链接而且检查是这个工作的翻译动画与否以及一些修改后,这是工作的动画了。!
After searching for a time i found this link and check is this working for Translate Animation or not and after some modification this is working for your animation too.!
请参阅以下修改code:
See modified code below:
public class TranslateAnim extends TranslateAnimation{
public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta,
float toYDelta) {
super(fromXDelta, toXDelta, fromYDelta, toYDelta);
// TODO Auto-generated constructor stub
}
private long mElapsedAtPause=0;
private boolean mPaused=false;
@Override
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if(mPaused && mElapsedAtPause==0) {
mElapsedAtPause=currentTime-getStartTime();
}
if(mPaused)
setStartTime(currentTime-mElapsedAtPause);
return super.getTransformation(currentTime, outTransformation);
}
public void pause() {
mElapsedAtPause=0;
mPaused=true;
}
public void resume() {
mPaused=false;
}
}
我只改类名称,扩展类名和这个类的构造函数。
I'll only change class name, extends class name and constructor of this class.
您可以使用它像:
TranslateAnim set1, set2, set3, set4; // objects of TranslateAnim Class
set1 = new TranslateAnim(-4, 10, -110, 0); // initialize all objects like this way
animSet.addAnimation(set1); // add all animation objests in your animation set as you do before
animSet.setFillAfter(true);
和启动动画之后,你只叫暂停和恢复方法。由于约翰的分享他的code和我们在一起。
and after start your animation you have only call pause and resume methods.Thanks to Johan for share his code with us.
希望这会解决您的问题。 :)
Hope this solve your problem. :)
这篇关于暂停和恢复翻译动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!