簡體   English   中英

如何在Android中從左到右動畫效果開始啟動活動(第一次活動)

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

我在android中做了一個活動,我想當我啟動該應用程序時,我的應用程序中的第一個活動應該是從左向右滑動動畫效果。但是我不知道如何實現它,所以有人可以幫我嗎或給我一些技巧,以便我解決。我在項目中准備了動畫XML。

預先感謝您我的代碼:

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

    }


}

在您的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>

然后在主題(themes.xml)中設置此樣式,如下所示:

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

然后,您只需將這些主題設置為AndroidManifest.xml中的每個活動,如下所示:

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

用最外面的布局名稱替換“視圖”。 在您希望動畫發生的活動的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);

將活動的背景色設置為白色(#FFF),創建一個imageview,並且該imageview應該是透明的,然后使用所需的可繪制數量編寫XML動畫。 最后在imageview中開始動畫。 這應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM