没有动画使用方法overridePendingTransition();

时间:2016-07-12 21:03:26

标签: android android-layout listview android-studio animation

切换活动时无法看到动画 第1活动--------------第2活动              没有动画 这是The Snippet

    private void StartAnimations() {
    Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
    anim.reset();
    LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
    l.clearAnimation();
    l.startAnimation(anim);

    anim = AnimationUtils.loadAnimation(this, R.anim.translate);
    anim.reset();
    ImageView iv = (ImageView) findViewById(R.id.splash);
    iv.clearAnimation();
    iv.startAnimation(anim);

    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                // Splash screen pause time
                while (waited < 3500) {
                    sleep(100);
                    waited += 100;
                }
                **Intent intent = new Intent(SplashScreen.this,
                        MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                overridePendingTransition(R.anim.left_in,R.anim.right_in);**

                SplashScreen.this.finish();
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                SplashScreen.this.finish();
            }

        }
    };
    splashTread.start();

}

这是我的动画文件

slide_left_in.xml

     <set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="-100%" android:toXDelta="0%"/>
<alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />

slide_right_out.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="100%"/>
<alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />

所以基本上OverridePendingTransition不做任何事情

3 个答案:

答案 0 :(得分:1)

你可以使用这个,因为我使用过这个。不要使用AnimationUtils,因为在Activity中,它只需要overridePendingTransition ...

public class SplashActivity extends AppCompatActivity {

    private static final int SPLASH_TIME = 3000;
    AppPreference preference;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        preference=new AppPreference(getApplicationContext());

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if(preference.getLoggedIn()){
                    Intent intent=new Intent(SplashActivity.this,MenuActivity.class);
                    startActivity(intent);
                    overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
                }
                else{
                    Intent intent=new Intent(SplashActivity.this,LaunchActivity.class);
                    startActivity(intent);
                    overridePendingTransition( R.anim.slide_in_up, R.anim.slide_out_up );
                }


                finish();
            }
        },SPLASH_TIME);
    }
}

答案 1 :(得分:0)

首先将函数中的名称更改为 overridePendingTransition(R.anim.slide_left_in,R.anim.slide_right_in);

而不是

overridePendingTransition(R.anim.left_in,R.anim.right_in);

并删除 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

答案 2 :(得分:0)

SplashScreen.this.finish();
overridePendingTransition()之前

并删除

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

像这样:

Intent intent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(intent);
SplashScreen.this.finish();
overridePendingTransition(R.anim.left_in,R.anim.right_in);