很简单。我也在我的聊天应用程序中添加了它,它的效果非常好。在你的根布局中,添加这一行:
android:animateLayoutChanges="true"
这将会使所有布局变化都具有动画效果。
如果以上方法没有帮助,你可以尝试以下操作。
userIdInput.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_ENTER) {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() - 40, edMESSAGE.getHeight() + 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
return false;
}
});
当按下新行键时,您可以在onKeyListener
中调用此方法。
有时,在输入文本时,会进入新的一行。为了实现动画效果,我们可以这样做:
chatInputET.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String string = s.toString();
if (string.length() > 0 && string.charAt(string.length() - 1) == '\n') {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() - 40, edMESSAGE.getHeight() + 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
}
});
至于退格键,您可以这样做:
inputMessage.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
String string = inputMessage.getText().toString();
if ((keyCode == KeyEvent.KEYCODE_DEL && (string.length() > 0 && string.charAt(string.length() - 1)) {
ValueAnimator anim = ValueAnimator.ofInt(edMESSAGE.getHeight() + 40, edMESSAGE.getHeight() - 40)
.setDuration(500);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
edMESSAGE.getLayoutParams().height = (int)animation.getAnimatedValue();
edMESSAGE.requestLayout();
}
});
anim.start();
}
return false;
}
});
android:animateLayoutChanges="true"
,但它并没有添加动画效果。 - mathematics-and-caffeineKEYCODE_ENTER
不足以满足需求。此外,这还缺少另一方面的情况,即当删除一个新行时,EditText必须收缩。 - mathematics-and-caffeinebinding.message.addTextChangedListener(new TextWatcher() {
int linesCount = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().length() == 0){
binding.message.getLayoutParams().height = defEditTextHeight;
linesCount = binding.message.getLineCount();
return;
}
if (linesCount == 0){
linesCount = binding.message.getLineCount();
binding.message.getLayoutParams().height = defEditTextHeight;
return;
}
if (linesCount < binding.message.getLineCount()) {
ValueAnimator anim = ValueAnimator.ofInt(binding.message.getHeight(), binding.message.getHeight() + 60)
.setDuration(250);
anim.addUpdateListener(animation -> {
binding.message.getLayoutParams().height = (int) animation.getAnimatedValue();
binding.message.requestLayout();
});
anim.start();
}else if( linesCount > binding.message.getLineCount()){
ValueAnimator anim = ValueAnimator.ofInt(binding.message.getHeight(), binding.message.getHeight() - 60)
.setDuration(250);
anim.addUpdateListener(animation -> {
binding.message.getLayoutParams().height = (int) animation.getAnimatedValue();
binding.message.requestLayout();
});
anim.start();
}
linesCount = binding.message.getLineCount();
}
@Override
public void afterTextChanged(Editable s) {
}
});
defEditTextHeight
是默认高度的编辑文本,可以像这样获取:
defEditTextHeight = binding.message.getHeight();
希望能有所帮助
checkAnimation
方法。 - peshkira