2017-10-11 36 views
-1

在我的應用程序中,我有一個dialogFragment當用戶重置他的密碼,成功的結果後,我讓linearlayout持有三點動畫不可見,但我也需要取消動畫,因爲如果不成功,用戶可能會再試一次或如果linearlayout不可見,則動畫未觸發?如果看不到動畫,我需要從視圖中取消動畫嗎?

只是想知道動畫是否連續,如果視圖是不可見的或者不見了?

預先感謝您。

enter image description here enter image description here

public class ResetPasswordDialog extends android.support.v4.app.DialogFragment { 

private EditText emailInput; 
private ImageView firstDot, secondDot, thirdDot; 
private LinearLayout dotsLinear; 
private Handler handler; 
private Runnable runnable; 
private Animation anim; 
private Animation anim2; 
private Animation anim3; 
private Handler timeoutHandler; 
private Runnable stopLoadingRunnable; 
private TextView resetPassword; 

public static ResetPasswordDialog newInstance() { 

    Bundle args = new Bundle(); 

    ResetPasswordDialog fragment = new ResetPasswordDialog(); 
    fragment.setArguments(args); 
    return fragment; 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.reset_password, container, false); 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    emailInput = (EditText) view.findViewById(R.id.et_email); 
    firstDot = (ImageView) view.findViewById(R.id.first_dot); 
    secondDot = (ImageView) view.findViewById(R.id.second_dot); 
    thirdDot = (ImageView) view.findViewById(R.id.third_dot); 
    dotsLinear = (LinearLayout) view.findViewById(R.id.dotsLinear); 

    resetPassword = (TextView) view.findViewById(R.id.reset_password); 
    initAnimation(); 

    timeoutHandler = new Handler(); 
    stopLoadingRunnable = new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getActivity(), R.string.reset_password_failed, Toast.LENGTH_SHORT).show(); 
      stopDotsAnimation(); 
      resetPassword.setVisibility(View.VISIBLE); 
     } 
    }; 

    resetPassword.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (ApiUtility.isEmailValid(emailInput.getText().toString())) { 
       startDotsAnimation(); 
       resetPassword.setVisibility(View.INVISIBLE); 
       timeoutHandler.postDelayed(stopLoadingRunnable, 15000); 
       dotsLinear.setVisibility(View.VISIBLE); 
       emailInput.clearFocus(); 
       ApiUtility.checkCustomerEmail(emailInput.getText().toString(), new ApiUtility.AuthenticationDelegate() { 
        @Override 
        public void invoke(MeetsCustomer meetsCustomer, Exception e) { 
         if (e == null) { 
          ((LogInFragment) getTargetFragment()).attemptToResetPassword(emailInput.getText().toString()); 
         } else if (e.toString().equals("java.lang.Exception: Customer not found")) { 
          resetPassword.setVisibility(View.VISIBLE); 
          removeTimeOutHandler(); 
          stopDotsAnimation(); 
          Toast.makeText(PlatformApplication.context, R.string.customer_not_found, Toast.LENGTH_SHORT).show(); 
         } else { 
          removeTimeOutHandler(); 
          stopDotsAnimation(); 
          resetPassword.setVisibility(View.VISIBLE); 
          Toast.makeText(PlatformApplication.context, R.string.reset_password_failed, Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
      } else { 
       Toast.makeText(getActivity(), R.string.invalid_email, Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 

private void initAnimation() { 
    anim = new AlphaAnimation(0f, 1f); 
    anim.setDuration(600); 

    anim2 = new AlphaAnimation(0f, 1f); 
    anim2.setDuration(600); 
    anim2.setStartOffset(200); 

    anim3 = new AlphaAnimation(0f, 1f); 
    anim3.setDuration(600); 
    anim3.setStartOffset(400); 
} 

private void startDotsAnimation() { 
    handler = new Handler(); 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      firstDot.startAnimation(anim); 
      secondDot.startAnimation(anim2); 
      thirdDot.startAnimation(anim3); 
      handler.postDelayed(this, 1000); 
     } 
    }; 
    handler.post(runnable); 
} 

public void stopDotsAnimation() { 
    handler.removeCallbacks(runnable); 
    firstDot.getAnimation().cancel(); 
    secondDot.getAnimation().cancel(); 
    thirdDot.getAnimation().cancel(); 
    dotsLinear.setVisibility(View.INVISIBLE); 
} 

public void removeTimeOutHandler() { 
    timeoutHandler.removeCallbacks(stopLoadingRunnable); 
} 
} 
+0

你可以用你的動畫代碼編輯你的答案來檢查它嗎? – tamtom

回答

0

我做了一些調試,它發現,如果視圖是不可見的動畫不工作。

0

可以設置取消或停止動畫裏面方法的onResume(),或在onStart(),創建活動時,這些方法被調用。欲瞭解更多詳細信息,你可以檢查Activity Lifecycle