简体   繁体   English

如何在Android中从左到右动画效果开始启动活动(第一次活动)

[英]How to start splash activity(very first activity) from left to right animation effect in android

I have made an activity in android,I want is when i start the app the very first activity in my app should come from left and slide to right animation effect.but i have no idea about how to implement it,So can anyone help me or give me some trick so that i can solve it out.I have animation XML ready with my project. 我在android中做了一个活动,我想当我启动该应用程序时,我的应用程序中的第一个活动应该是从左向右滑动动画效果。但是我不知道如何实现它,所以有人可以帮我吗或给我一些技巧,以便我解决。我在项目中准备了动画XML。

Thank you in advance my code: 预先感谢您我的代码:

package com.esp.Estorec.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

public class SplashActivity1 extends Activity implements AnimationListener {
RelativeLayout view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    view=(RelativeLayout)findViewById(R.id.splash1);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash1);
        Animation animationSlideInLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);

        animationSlideInLeft.setAnimationListener(new AnimationListener(){
            @Override
            public void onAnimationEnd(Animation animation) {
                // if you need to do something
            }

            @Override
            public void onAnimationRepeat(Animation animation) {    
            }

            @Override
            public void onAnimationStart(Animation animation) {
        }});

        view.startAnimation(animationSlideInLeft);


        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                handler.sendEmptyMessage(1);
            }
        }, 2000);
    }

    private Handler handler = new Handler()
    {
        @SuppressWarnings("deprecation")
        @Override
        public void handleMessage(android.os.Message msg)
        {
            try
            {
                Intent intent = null;
                intent = new Intent(SplashActivity1.this,
                        SplashActivity2.class);
                startActivity(intent);
                overridePendingTransition(R.anim.animated_activity_slide_left_in, R.anim.animated_activity_slide_right_out);
                finish();
            } catch (Exception e) {

            }
        }
    };
    @Override
    public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }


}

Define a new animation style in your styles.xml, as follows 在您的styles.xml中定义新的动画样式,如下所示

    <style name="MyTheme.Window" parent="@android:style/Animation.Activity">
      <item name="android:activityOpenEnterAnimation">@anim/your_desired_animation</item>
      <item name="android:activityOpenExitAnimation">@anim/your_desired_animation</item>
      <item name="android:activityCloseEnterAnimation">@anim/your_desired_animation</item>
      <item name="android:activityCloseExitAnimation">@anim/your_desired_animation</item>
    </style>

Then set this style in a theme (themes.xml) as follows : 然后在主题(themes.xml)中设置此样式,如下所示:

<style name="MyTheme" parent="@android:style/Theme">
      <item name="android:windowAnimationStyle">@style/MyTheme.Window</item>
</style>

And then you can simply set these themes to every activity in your AndroidManifest.xml as follows: 然后,您只需将这些主题设置为AndroidManifest.xml中的每个活动,如下所示:

<activity 
  android:name="your_activity" 
  android:theme="@style/MyTheme">
</activity>

Replace "view" with your outermost Layout name. 用最外面的布局名称替换“视图”。 Write this code in your oncreate method of the activity that you want the animation to occur. 在您希望动画发生的活动的oncreate方法中编写此代码。

 animationSlideInLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
 animationSlideInLeft.setDuration(1500);

            animationSlideInLeft.setAnimationListener(new AnimationListener(){
                @Override
                public void onAnimationEnd(Animation animation) {
                    // if you need to do something
                }

                @Override
                public void onAnimationRepeat(Animation animation) {    
                }

                @Override
                public void onAnimationStart(Animation animation) {
            }});

            view.startAnimation(animationSlideInLeft);

Set the background colour of your activity to white(#FFF), create an imageview and the imageview should be transparent and then write a XML animation using the amount of drawables you want. 将活动的背景色设置为白色(#FFF),创建一个imageview,并且该imageview应该是透明的,然后使用所需的可绘制数量编写XML动画。 Finally start the animation in the imageview. 最后在imageview中开始动画。 This should do the trick. 这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM