Android布局过渡动画异常求助:加载动画无法正常工作
问题:Android登录/注册/密码重置布局过渡动画异常,加载动画无法正常运行
功能整体正常,但布局显示(VISIBLE)、隐藏(GONE)的过渡环节存在异常,尤其是加载动画无法正常运行,多次调试未解决,需排查代码中的动画问题:
if (edittext1.getText().toString().equals("")) { textview3.setTextColor(0xFFFF1744); final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(linear3, "backgroundColor", new ArgbEvaluator(), 0xffEEEEEE, 0xffFF1744); backgroundColorAnimator.setDuration(500); backgroundColorAnimator.start(); } else { if (textview6.getText().toString().equals("Get password reset link")) { _telegramLoaderDialog(true); firebaseAuth.sendPasswordResetEmail(edittext1.getText().toString()).addOnCompleteListener(_firebaseAuth_reset_password_listener); } else { if (edittext2.getText().toString().equals("")) { textview4.setTextColor(0xFFFF1744); final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(linear4, "backgroundColor", new ArgbEvaluator(), 0xffEEEEEE, 0xffFF1744); backgroundColorAnimator.setDuration(500); backgroundColorAnimator.start(); } else { if (Current) { if (edittext3.getText().toString().equals("")) { textview9.setTextColor(0xFFFF1744); final ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(linear7, "backgroundColor", new ArgbEvaluator(), 0xffEEEEEE, 0xffFF1744); backgroundColorAnimator.setDuration(500); backgroundColorAnimator.start(); } else { _autoTransitionScroll(vscroll1); linear8.setVisibility(View.VISIBLE); textview6.setVisibility(View.GONE); textview7.setEnabled(false); checkbox1.setEnabled(false); edittext1.setEnabled(false); edittext2.setEnabled(false); edittext3.setEnabled(false); if (edittext2.getText().toString().equals(edittext3.getText().toString())) { if (checkbox1.isChecked()) { firebaseAuth.createUserWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(AuthActivity.this, _firebaseAuth_create_user_listener); } else { _autoTransitionScroll(vscroll1); linear8.setVisibility(View.GONE); textview6.setVisibility(View.VISIBLE); textview7.setEnabled(true); checkbox1.setEnabled(true); edittext1.setEnabled(true); edittext2.setEnabled(true); edittext3.setEnabled(true); LayoutInflater Inflater = getLayoutInflater(); View InfView = getLayoutInflater().inflate(R.layout.success_toast, null); LinearLayout border = (LinearLayout) InfView.findViewById(R.id.border); LinearLayout linear3 = (LinearLayout) InfView.findViewById(R.id.linear3); LinearLayout line = (LinearLayout) InfView.findViewById (R.id.line); TextView title = (TextView) InfView.findViewById(R.id.title); TextView content = (TextView) InfView.findViewById(R.id.content); ImageView image_logo = (ImageView) InfView.findViewById(R.id.image_logo); Toast ToastName = Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT); ToastName.setView(InfView); ToastName.show(); image_logo.setImageResource(R.drawable.info1); title.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_med.ttf"), 1); content.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_light.ttf"), 0); _RoundAndBorder(border, "#2F87EB", 0, "#ffffff", 8); _SX_CornerRadius_4(line, "#80ffffff", "#80ffffff", 0, 0, 8, 0, 8); title.setText("Attention Please!"); content.setText("Agree with our Privacy Policy and T&Cs to continue creating an account."); } } else { _autoTransitionScroll(vscroll1); linear8.setVisibility(View.GONE); textview6.setVisibility(View.VISIBLE); textview7.setEnabled(true); checkbox1.setEnabled(true); edittext1.setEnabled(true); edittext2.setEnabled(true); edittext3.setEnabled(true); LayoutInflater Inflater = getLayoutInflater(); View InfView = getLayoutInflater().inflate(R.layout.success_toast, null); LinearLayout border = (LinearLayout) InfView.findViewById(R.id.border); LinearLayout linear3 = (LinearLayout) InfView.findViewById(R.id.linear3); LinearLayout line = (LinearLayout) InfView.findViewById (R.id.line); TextView title = (TextView) InfView.findViewById(R.id.title); TextView content = (TextView) InfView.findViewById(R.id.content); ImageView image_logo = (ImageView) InfView.findViewById(R.id.image_logo); Toast ToastName = Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT); ToastName.setView(InfView); ToastName.show(); image_logo.setImageResource(R.drawable.info1); title.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_med.ttf"), 1); content.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_light.ttf"), 0); _RoundAndBorder(border, "#2F87EB", 0, "#ffffff", 8); _SX_CornerRadius_4(line, "#80ffffff", "#80ffffff", 0, 0, 8, 0, 8); title.setText("Attention Please!"); content.setText("The Passwords you've entered does not match! Please check and try again."); } } } else { firebaseAuth.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(AuthActivity.this, _firebaseAuth_sign_in_listener); _autoTransitionScroll(vscroll1); linear8.setVisibility(View.VISIBLE); textview6.setVisibility(View.GONE); textview7.setEnabled(false); checkbox1.setEnabled(false); edittext1.setEnabled(false); edittext2.setEnabled(false); edittext3.setEnabled(false); } } } }
问题排查与修复方案
1. 布局显隐无平滑过渡
当前直接调用setVisibility()切换布局状态,未触发系统过渡动画。需用TransitionManager触发自动过渡:
- 替换
_autoTransitionScroll(vscroll1);为:
TransitionManager.beginDelayedTransition(vscroll1);
- 确保
vscroll1(滚动布局)已启用过渡,在布局XML中添加:
android:animateLayoutChanges="true"
2. Toast内容设置顺序错误
代码中先调用ToastName.show()再设置文本、图标等内容,导致Toast显示空布局。调整顺序为先完成所有内容设置,最后调用show():
// 先完成所有View配置 image_logo.setImageResource(R.drawable.info1); title.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_med.ttf"), 1); content.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/en_light.ttf"), 0); _RoundAndBorder(border, "#2F87EB", 0, "#ffffff", 8); _SX_CornerRadius_4(line, "#80ffffff", "#80ffffff", 0, 0, 8, 0, 8); title.setText("Attention Please!"); content.setText("Agree with our Privacy Policy and T&Cs to continue creating an account."); // 最后显示Toast ToastName.setView(InfView); ToastName.show();
3. 加载动画(linear8)未正确触发
- 若
linear8内部包含ProgressBar或自定义加载动画,需在设置VISIBLE时启动动画:// 示例:启动ProgressBar无限旋转动画 ProgressBar progressBar = linear8.findViewById(R.id.loading_progress); ObjectAnimator rotation = ObjectAnimator.ofFloat(progressBar, "rotation", 0f, 360f); rotation.setDuration(1000); rotation.setRepeatCount(ValueAnimator.INFINITE); rotation.start(); - 异步操作完成后,在
OnCompleteListener中隐藏linear8并恢复控件状态,同样用TransitionManager触发过渡:private OnCompleteListener<AuthResult> _firebaseAuth_sign_in_listener = task -> { TransitionManager.beginDelayedTransition(vscroll1); linear8.setVisibility(View.GONE); textview6.setVisibility(View.VISIBLE); // 恢复控件交互状态 textview7.setEnabled(true); checkbox1.setEnabled(true); edittext1.setEnabled(true); edittext2.setEnabled(true); edittext3.setEnabled(true); // 处理登录结果逻辑... };
4. 重复创建ObjectAnimator优化
多次重复创建背景色动画,可封装为工具方法减少冗余:
private void animateErrorBackground(View view) { ObjectAnimator backgroundColorAnimator = ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), 0xffEEEEEE, 0xffFF1744); backgroundColorAnimator.setDuration(500); backgroundColorAnimator.start(); }
调用时直接用animateErrorBackground(linear3);替代重复代码。
内容的提问来源于stack exchange,提问作者Suman Dey
相关产品推荐
相关产品推荐

