我正在使用Android应用程序,当我点击按钮时,我的动画无效。
这是我的代码,
//Login layout signup button click listener to hide login layout and show signup layout
loginUserSignupBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivityActionFadeInAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
signupDetailsLayout.startAnimation(startActivityActionSlideUpAnim);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startActivityActionFadeOutAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
loginDetailsLayout.setAnimation(startActivityActionSlideDownAnim);
}
@Override
public void onAnimationEnd(Animation animation) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
//We are doing nothing here
System.out.println(e.toString());
}
finally {
signupDetailsLayout.startAnimation(startActivityActionFadeInAnim);
}
}
@Override
public void onAnimationRepeat (Animation animation){
}
});
loginDetailsLayout.setAnimation(startActivityActionFadeOutAnim);
Toast.makeText(StartActivity.this, "Signup clicked", Toast.LENGTH_LONG).show();
}
});
Toast工作正常,但我正在尝试的动画无效。
这是我的xml布局文件,
<RelativeLayout
android:id="@+id/startActivityAppLogoLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:padding="5dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startActivityAppLogo"
android:layout_alignParentTop="true"
android:src="@mipmap/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/startActivityAppLogo"
android:text="@string/start_activity_app_title"
android:layout_marginTop="10sp"
android:gravity="center"
android:id="@+id/startActivityAppTitleTV"
android:textColor="#7f8c8d"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginDetailsLayout"
android:gravity="center"
android:layout_centerInParent="true"
android:padding="5dp"
android:visibility="invisible"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userLoginNameET"
android:ems="12"
android:hint="@string/login_user_name_edit"
android:textColor="#1abc9c"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="12"
android:hint="@string/login_user_pass_et"
android:id="@+id/userLoginPasswordET"
android:layout_below="@+id/userLoginNameET"
android:layout_marginTop="20dp"
android:textColor="#1abc9c"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_user_login_btn"
android:id="@+id/userLoginButtonLogin"
style="?android:buttonBarButtonStyle"
android:padding="5dp"
android:textColor="#D24D57"
android:textAllCaps="false"
android:layout_below="@+id/userLoginPasswordET"
android:layout_alignLeft="@+id/userLoginPasswordET"
android:layout_alignStart="@+id/userLoginPasswordET"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="@string/login_user_forgot_tv"
android:layout_below="@id/userLoginButtonLogin"
android:textColor="#000000"
android:id="@+id/userLoginForgotTV"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_user_signup_btn"
android:id="@+id/userLoginButtonSignup"
style="?android:buttonBarButtonStyle"
android:padding="5dp"
android:textColor="#c0392b"
android:textAllCaps="false"
android:layout_below="@+id/userLoginPasswordET"
android:layout_alignRight="@+id/userLoginPasswordET"
android:layout_alignEnd="@+id/userLoginPasswordET"
android:textSize="18sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/signupDetailsLayout"
android:gravity="center"
android:layout_centerInParent="true"
android:padding="5dp"
android:visibility="invisible"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userSignupNameET"
android:ems="12"
android:hint="@string/signup_user_name_edit"
android:textColor="#1abc9c"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="12"
android:id="@+id/userSignupEmailET"
android:layout_marginTop="20dp"
android:hint="@string/signup_user_email_et"
android:layout_below="@+id/userSignupNameET"
android:textColor="#1abc9c"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="12"
android:hint="@string/login_user_pass_et"
android:id="@+id/userSignupPasswordET"
android:layout_below="@+id/userSignupEmailET"
android:layout_marginTop="20dp"
android:textColor="#1abc9c"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="12"
android:id="@+id/userSignupPhoneET"
android:hint="@string/signup_user_mobile_et"
android:layout_below="@+id/userSignupPasswordET"
android:layout_marginTop="20dp"
android:textColor="#1abc9c"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_user_login_btn"
android:id="@+id/userSignupButtonLogin"
style="?android:buttonBarButtonStyle"
android:padding="5dp"
android:textColor="#c0392b"
android:textAllCaps="false"
android:layout_below="@+id/userSignupPhoneET"
android:layout_alignLeft="@+id/userSignupPhoneET"
android:layout_alignStart="@+id/userSignupPhoneET"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="@string/signup_user_terms_tv"
android:layout_below="@id/userSignupButtonLogin"
android:textColor="#000000"
android:id="@+id/userSignupTermsTV"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_user_signup_btn"
android:id="@+id/userSignupButtonSignup"
style="?android:buttonBarButtonStyle"
android:padding="5dp"
android:textColor="#D24D57"
android:textAllCaps="false"
android:layout_below="@+id/userSignupPhoneET"
android:layout_alignRight="@+id/userSignupPhoneET"
android:layout_alignEnd="@+id/userSignupPhoneET"
android:textSize="18sp"/>
</RelativeLayout>
我也尝试过创建一个函数,但它也没有用,代码是
//Login layout signup button click listener to hide login layout and show signup layout
loginUserSignupBTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
activateLayoutAnimation(signupDetailsLayout, loginDetailsLayout);
//Toast.makeText(StartActivity.this, "Signup clicked", Toast.LENGTH_LONG).show();
}
});
和功能是,
//For action layout animations
private void activateLayoutAnimation(final RelativeLayout showLayout, final RelativeLayout hideLayout){
startActivityActionFadeInAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
showLayout.startAnimation(startActivityActionSlideUpAnim);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
startActivityActionFadeOutAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
hideLayout.setAnimation(startActivityActionSlideDownAnim);
}
@Override
public void onAnimationEnd(Animation animation) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
//We are doing nothing here
System.out.println(e.toString());
}
finally {
showLayout.startAnimation(startActivityActionFadeInAnim);
}
}
@Override
public void onAnimationRepeat (Animation animation){
}
});
hideLayout.setAnimation(startActivityActionFadeOutAnim);
}
感谢您的时间和帮助朋友。
答案 0 :(得分:2)
setAnimation不会立即启动动画。要立即开始,请改为调用startAnimation。