题
我正在尝试同时在Android上进行几次翻译。
我在布局上有2个或更多按钮(所有相同尺寸),当我按一个按钮时,我希望其他按钮移出屏幕。
我已经完成了一个测试应用程序来尝试实现此行为。
在它上,我在单击一个按钮上设置了一个侦听器进行测试,例如:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button toMove = (Button) findViewById(R.id.button_test2);
Button toMove2 = (Button) findViewById(R.id.button_test3);
AnimationSet set = new AnimationSet(true);
TranslateAnimation anim = new TranslateAnimation(0, -toMove
.getWidth(), 0, 0);
anim.setFillAfter(true);
anim.setDuration(1000);
toMove.setAnimation(anim);
toMove2.setAnimation(anim);
set.addAnimation(anim);
set.startNow();
}
风景:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button_test" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello" />
<Button android:id="@+id/button_test2" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello"/>
<Button android:id="@+id/button_test3" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello"/>
</LinearLayout>
问题是两个按钮启动了动画,一个按钮又又一个接一个。我读到这是由于 getDelayForView()
这返回每个延迟的延迟。有什么方法可以同时移动2个或更多按钮?
Google不是很有帮助: -
解决方案
问题:
看起来 setAnimation
将在ac启动动画,并可能异步。但是,为第二视图设置动画可能会锁定。必须有一个调度程序,因为以不同顺序设置按钮的动画不会影响底部更快的事实。
解决方案是通过创建两个单独的动画来防止这种假设的锁定。
代码:
public void onClick(View view) {
Button toMove = (Button) findViewById(R.id.button_test2);
Button toMove2 = (Button) findViewById(R.id.button_test3);
TranslateAnimation anim = new TranslateAnimation(0, -toMove
.getWidth(), 0, 0);
anim.setFillAfter(true);
anim.setDuration(1000);
TranslateAnimation anim2 = new TranslateAnimation(0, -toMove
.getWidth(), 0, 0);
anim2.setFillAfter(true);
anim2.setDuration(1000);
//THERE IS ONE MORE TRICK
toMove.setAnimation(anim);
toMove2.setAnimation(anim2);
}
笔记:
在里面 //THERE IS ONE MORE TRICK
, ,您可以添加以下代码以确保它们一起移动。 仍然必须有1毫秒左右的滞后。
long time =AnimationUtils.currentAnimationTimeMillis();
//This invalidate is needed in new Android versions at least in order for the view to be refreshed.
toMove.invalidate();
toMove2.invalidate();
anim.setStartTime(time);
anim2.setStartTime(time);
不隶属于 StackOverflow