重新启动Android Activity后动画未显示

时间:2015-12-12 05:59:58

标签: java android android-studio android-animation

我会尽力在没有视频的情况下解释我的问题

我有一个登录活动,成功登录后,EditText和Button字段淡出,“登录”TextView淡入(使用Facebook Shimmer)。这很棒!!但是,成功登录后,我们会受到空白活动的欢迎(仍然很好)。现在,我覆盖了后退按钮,以便在按下后退按钮时,用户被迫再次登录。我的问题出现在用户此时点击登录按钮。编辑文本和按钮元素很好地淡出,但“登录”TextView永远不会淡入。

以下是一些图片示例。我还将发布LoginActivity.class的源代码,如果您想下载该项目,请访问:git@github.com:fbgrecojr/Android-Application-Login-Activity-Template.git

如果您下载项目,请输入用户名:testuser和密码:testpass将起作用。

图片:

  1. 初次登录(工作) enter image description here
  2. enter image description here

    1. 登录尝试按下后退按钮然后再次登录(我覆盖以重新启动意图)
    2. enter image description here

      LoginActivity.class

      package com.projects.fbgrecojr.logintemplate.UI;
      
      import android.content.Context;
      import android.content.Intent;
      import android.net.ConnectivityManager;
      import android.net.NetworkInfo;
      import android.os.AsyncTask;
      import android.os.Handler;
      import android.support.design.widget.TextInputLayout;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.view.ViewGroup;
      import android.view.animation.AccelerateInterpolator;
      import android.view.animation.AlphaAnimation;
      import android.view.animation.Animation;
      import android.view.animation.AnimationUtils;
      import android.view.animation.DecelerateInterpolator;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.LinearLayout;
      import android.widget.RelativeLayout;
      import android.widget.Toast;
      
      import com.facebook.shimmer.ShimmerFrameLayout;
      import com.projects.fbgrecojr.logintemplate.HTTPManager.HttpManager;
      import com.projects.fbgrecojr.logintemplate.HTTPManager.RequestPackage;
      import com.projects.fbgrecojr.logintemplate.Parser.JSONParser;
      import com.projects.fbgrecojr.logintemplate.R;
      import com.projects.fbgrecojr.logintemplate.Session.Session;
      import com.projects.fbgrecojr.logintemplate.Structures.User;
      import com.projects.fbgrecojr.logintemplate.Utility.UTILITY;
      
      
      /**
       * An example full-screen activity that shows and hides the system UI (i.e.
       * status bar and navigation/system bar) with user interaction.
       */
      public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
      
          private EditText userName, password;
          private Button login;
          private RelativeLayout image;
          private LinearLayout button, belowPic;
          private Animation fadeInImage, fadeInButton, bottomUp, fadeOut;
          private TextInputLayout inputLayoutName,inputLayoutPassword;
          private ViewGroup hiddenPanel;
          private ShimmerFrameLayout container, loggingIn;
          private static final int SECOND = 1000;
          private static final int HALF_SECOND = 500;
          private static final int QUARTER_SECOND = 250;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_login);
      
              //INITIALIZE ANIMATION ITEMS
              fadeInImage = new AlphaAnimation(0, 1);
              fadeInButton = new AlphaAnimation(0, 1);
              fadeOut = new AlphaAnimation(1.0f,0.0f);
              bottomUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bottom_up_animation);
              fadeInImage.setInterpolator(new AccelerateInterpolator()); //and this
              bottomUp.setInterpolator(new DecelerateInterpolator());
      
              //GET UI ELEMENTS
              userName = (EditText) findViewById(R.id.userName);
              password = (EditText) findViewById(R.id.password);
              login = (Button) findViewById(R.id.login);
              image = (RelativeLayout) findViewById(R.id.image);
              button = (LinearLayout) findViewById(R.id.button);
              container = (ShimmerFrameLayout) findViewById(R.id.shimmer);
              belowPic = (LinearLayout) findViewById(R.id.below_picture);
              loggingIn = (com.facebook.shimmer.ShimmerFrameLayout) findViewById(R.id.login_shimmer);
              hiddenPanel = (ViewGroup)findViewById(R.id.input);
              inputLayoutName = (TextInputLayout) findViewById(R.id.text_input_username);
              inputLayoutPassword = (TextInputLayout) findViewById(R.id.text_input_password);
      
              //SET UI PROPERTIES
              loggingIn.setVisibility(View.INVISIBLE);
              userName.setCursorVisible(false);
              password.setCursorVisible(false);
              password.setHint("Password");
              userName.setHint("Username");
              new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      userName.setCursorVisible(true);
                      password.setCursorVisible(true);
                      userName.requestFocus();
                  }
              }, LoginActivity.SECOND * 3);
      
              //ANIMATIONS
              fadeInImage.setDuration(SECOND * 3);
              fadeOut.setStartOffset(SECOND);
              fadeOut.setDuration(SECOND);
              image.setAnimation(fadeInImage);
              fadeInButton.setStartOffset(SECOND + HALF_SECOND + QUARTER_SECOND);
              fadeInButton.setDuration(SECOND * 2);
              button.setAnimation(fadeInButton);
              hiddenPanel.startAnimation(bottomUp);
              hiddenPanel.setVisibility(View.VISIBLE);
              container.setDuration(SECOND * 2 + QUARTER_SECOND);
              container.setRepeatDelay(QUARTER_SECOND);
              container.setIntensity((float) 0.15);
              container.setBaseAlpha((float) 0.75);
              container.setFadingEdgeLength(3);
              container.setDropoff((float) 0.40);
              container.startShimmerAnimation();
      
              //ON CLICK LISTENERS
              login.setOnClickListener(this);
          }
      
          public void onClick(View v) {
              switch (v.getId()) {
                  case R.id.login:
      
                      if(getUserName().getText().toString().equals("") || getUserName().getText().toString().equals(" ")) {
                          inputLayoutName.setError("enter username");
                      }else if(getPassword().getText().toString().equals("") || getPassword().getText().toString().equals(" ")){
                          inputLayoutPassword.setError("enter password");
                      }else{
                          //webservice
                          if (UTILITY.isOnline(getApplicationContext())) {
                              RequestPackage p = new RequestPackage();
                              p.setMethod("GET");
                              p.setUri(UTILITY.UBUNTU_SERVER_URL);
                              p.setParam("query", "user");
                              p.setParam("username", getUserName().getText().toString());
                              new WebserviceCallOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, p);
                          } else {
                              Toast.makeText(getApplicationContext(), "you are not connected to the internet", Toast.LENGTH_LONG).show();
                          }
                      }
                      break;
              }
          }
      
          private void animateExit() {
              //fade out annimation
              belowPic.startAnimation(fadeOut);
              belowPic.setVisibility(View.INVISIBLE);
              fadeInImage.setStartOffset(SECOND * 2);
              fadeInImage.setDuration(HALF_SECOND);
              loggingIn.startAnimation(fadeInImage);
              loggingIn.setVisibility(View.VISIBLE);
              loggingIn.setDuration(SECOND);
              loggingIn.startShimmerAnimation();
          }
      
          public EditText getPassword() {
              return password;
          }
      
          public EditText getUserName() {
              return userName;
          }
      
          private class WebserviceCallOne extends AsyncTask<RequestPackage, String, User> {
              @Override
              protected User doInBackground(RequestPackage... params) {
                  String content = HttpManager.getData(params[0]);
                  return JSONParser.parseUserFeed(content);
              }
      
              @Override
              protected void onPostExecute(User s) {
                  Session.setCurrentUser(s);
                  //if null, error stacktrace will print to the log. This is expected!!
                  if(Session.getCurrentUser() == null){ //username was incorrect
                      inputLayoutName.setError("username does not exist");
                  }else{ //check password
                      if(getPassword().getText().toString().equals(s.getPassword())){ //passwords match
                          animateExit();
                          new Handler().postDelayed(new Runnable() {
                              @Override
                              public void run() {
                                  startActivity(new Intent(getApplicationContext(), MainActivity.class));
                              }
                          },LoginActivity.SECOND * 4);
                      }else{
                          inputLayoutPassword.setError("password incorrect");
                      }
                  }
              }
          }
      }
      

      MainActivity.class

      public class MainActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          }
      
          /**
           * Take care of popping the fragment back stack or finishing the activity
           * as appropriate.
           */
          @Override
          public void onBackPressed() {
              startActivity(new Intent(this, LoginActivity.class));
          }
      }
      

2 个答案:

答案 0 :(得分:0)

你必须调用animateExit(); onResume methord中的代码

   @Override
    public void onResume() {
      super.onResume();
      animateExit();
    } 

答案 1 :(得分:0)

  1. 覆盖您的Activity中的onResume()。

    尝试在onresume上开始动画。

  2. 覆盖您的活动中的onPause()

    尝试在onPause中停止动画。

  3. 希望这有助于:)