Android动画仅适用于第一次

时间:2016-09-09 00:04:31

标签: java android android-studio animation alpha

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

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>

1 个答案:

答案 0 :(得分:1)

不要使用sleep()来延迟开始其他活动。你可以做你想做的事情

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);

这应该有效。