Android:动画后打开意图活动

时间:2016-02-27 05:08:53

标签: android animation android-intent

我想开始一项活动,并能够用它制作动画。所以在动画之后,意图应该开始,但是当我运行程序时,动画不会动画,它只会显示第一帧,然后等待几秒钟然后进入第二个活动,有没有办法可以完成动画首先去另一个活动?这是我的代码:

public void onTextChanged(CharSequence sa, int start,
                                  int before, int count) {
            if (sa.length() == 4) {
                final Intent Menu = new Intent(LogIn.this, Menu.class);
                progressDialog = ProgressDialog.show(LogIn.this, "", "Loading..");
                password = getPass("password", getApplicationContext());
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        if ((password.equals(""))) {
                            if (s.toString().equals("1234")) {

                                findBT();
                                try {
                                    openBT();
                                }
                                catch (IOException e) {}
                                beginListenForData();

                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        progressDialog.dismiss();
                                        connectedAnim();

                                        LogIn.this.startActivity(Menu);
                                    }
                                });
                                /*Intent Menu = new Intent(LogIn.this, Menu.class);
                                LogIn.this.startActivity(Menu);*/

                            } else {
                                Toast.makeText(LogIn.this, "Incorrect Password", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            realpass = getPass("password", getApplicationContext());
                            if (s.toString().equals(realpass)) {
                                findBT();

                            } else {
                                Toast.makeText(LogIn.this, "Incorrect Password", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                };
                new Thread(runnable).start();
            }
        }
    });

connectedAnim()

public void connectedAnim(){

    final Dialog dialog = new Dialog(LogIn.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.connected);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    IVcon = (ImageView)dialog.findViewById(R.id.IVcon);
    IVcon.setBackgroundResource(R.anim.connectedanim);

    final AnimationDrawable animcon = (AnimationDrawable)IVcon.getBackground();
    dialog.setCancelable(true);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            animcon.start();
        }
    });
    dialog.show();

    new Handler().postDelayed(new Runnable() {

        public void run() {

            dialog.dismiss();
            /*Intent Menu = new Intent(LogIn.this, Menu.class);
            Menu.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            LogIn.this.startActivity(Menu);*/
        }
    }, 1000);
}

1 个答案:

答案 0 :(得分:0)

创建全局类........

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;



    public class GlobalClass {



        public static void activitySlideBackAnimation(Activity context) {
            context.overridePendingTransition(R.anim.slide_back_left_to_right,
                    R.anim.slide_back_right_to_left);
        }

        public static void activitySlideForwardAnimation(Activity context) {
            context.overridePendingTransition(R.anim.slide_forward_left_to_right,
                    R.anim.slide_forward_right_to_left);
        }


    }

MainActivity.java ........................

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import static com.example.GlobalClass.activitySlideForwardAnimation;

    public class MainActivity extends Activity {

        Activity context;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_splash);
            context=this;

             startActivity(new Intent(context, NextActivity.class));
             activitySlideForwardAnimation(context);           
             finish();

        }
}

在res> anim文件夹

中添加以下xml文件

slide_forward_right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="-100%"
        android:toYDelta="0%" />

</set>

slide_forward_left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromXDelta="100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>

slide_back_right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />

</set>

slide_back_left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >

    <translate
        android:duration="300"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />

</set>