gpt4 book ai didi

java - Android 动画仅在第一次时有效

转载 作者:行者123 更新时间:2023-12-01 09:31:02 25 4
gpt4 key购买 nike

我正在测试我正在制作的应用程序。当应用程序启动时,启动屏幕应该在登录屏幕中执行淡入/淡出动画。当应用程序第一次启动时,动画效果很好。但是,一旦我从任务管理器中清除应用程序并重新启动它,动画就不会出现,它会直接进入登录屏幕,没有动画发生。附件是处理动画的代码部分。如果需要任何其他代码,我也会提供。我只是想要它,以便每次应用程序运行时动画都会运行。

SplashScreen.java

public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);



Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}

finally{
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}
};
timerThread.start();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

}

LoginActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
registerButton = (Button)findViewById(R.id.button);
registerButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent(LoginActivity.this,Register.class);
startActivity(intent);

overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);

}
});

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="2000" />

fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="2000" />

activity_login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.drinkuprewards.julian.drinkuprewards.LoginActivity"
android:background="#97007C">

<!-- Login progress -->

<ProgressBar
android:id="@+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />

<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="false">

</ScrollView>

<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">

<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/imageView"
android:src="@drawable/logosm"
android:contentDescription="@string/mainlogo" />


<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_email"
android:maxLines="1"
android:singleLine="true"
android:autoText="false"
android:background="#fef500"
android:textColor="#000000"
android:inputType="textEmailAddress" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spacer" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:autoText="false"
android:background="#fef500"
android:textColor="#000000" />

<Button
android:id="@+id/email_sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_register"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:background="#fef500"
android:textColor="#000000" />

<Button
style="?android:textAppearanceSmall"
android:id="@+id/button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:background="#fef500"
android:onClick="Register"
android:textColor="#000000" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/version_number"
android:id="@+id/textView"
android:textColor="#000000" />

</LinearLayout>

最佳答案

不要使用sleep()来延迟启动另一个 Activity 。你可以做你想做的事情

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashScreen.this,LoginActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
}, 3000);

这应该有效。

关于java - Android 动画仅在第一次时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com