我正在尝试为UI元素添加动画效果。 我想要将一个editText和一个按钮从中间移动到屏幕顶部,并在它们下面以表格的形式显示http调用的结果。 目前不确定应该使用Java还是XML来实现,希望有人能指点一下。谢谢。
使用翻译框架来实现此功能,操作方式如下:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
所以你需要编写移动视图在y轴方向的代码,如下:
mAnimation = new TranslateAnimation(0, 0, 0, 599);
mAnimation.setDuration(10000);
mAnimation.setFillAfter(true);
mAnimation.setRepeatCount(-1);
mAnimation.setRepeatMode(Animation.REVERSE);
view.setAnimation(mAnimation);
这里的view可以是任何东西,例如textview、imageView等。
被接受的答案在我的代码中引起了错误,下面的代码片段几乎与被接受的答案相同,并且在不引起错误的情况下将对象滑动到屏幕外。我需要将手势与键盘绑定,以便也可以“滑开”,并从TranslateAnimation切换到ObjectAnimator(下面的第二个代码块)。
final LinearLayout keyPad = (LinearLayout)findViewById(R.id.keyPad);
moveKeyPadUp(keyPad);
private void moveKeyPadUp(LinearLayout keyPad){
Animation animation = new TranslateAnimation(0,0,0,-500);
animation.setDuration(1000);
animation.setFillAfter(true);
keyPad.startAnimation(animation);
}
private void moveKeyPadUpdated(LinearLayout keyPad){
ObjectAnimator mover = ObjectAnimator.ofFloat(keyPad,"translationY",0,-500);
mover.setDuration(300);
mover.start();
}