2013-09-26 108 views
0

在我的短信應用程序中,我有一個對話框,當用戶點擊一個附加圖像來展開它時打開。Android - 動畫後的鬼點擊次數

一切正常,直到我決定添加動畫。該對話框有3個按鈕(右上角的一個關閉的ImageButton和對話框的底部的兩個按鈕)。當用戶點擊圖像時,按鈕動畫出/入。這一切都可以正常工作,除非按鈕在隱藏後仍然可以點擊。

動畫之前: Before

後動畫:

// Declare these here to increase scope. (Listeners) 
private static boolean ContainerChanged; 
private static boolean ActionsHidden; 
private static boolean AnimStarted; 
private static boolean closeAnimFinished; 
private static boolean barAnimFinished; 

private void showExpandedImageDialog(Uri imgUri) 
{ 
    // Initialize Dialog 
    final Dialog dialog = new Dialog(ActivitySend.this, R.style.FullHeightDialog); 
    dialog.setContentView(R.layout.dialog_attachment_image_send); 

    // Initialize Views 
    final RelativeLayout Container = (RelativeLayout) dialog.findViewById(R.id.Container); 
    final LinearLayout actions = (LinearLayout) dialog.findViewById(R.id.Actions); 
    final ImageButton btnClose = (ImageButton) dialog.findViewById(R.id.btnClose); 
    Button btnReplace = (Button) dialog.findViewById(R.id.btnReplace); 
    Button btnRemove = (Button) dialog.findViewById(R.id.btnRemove); 
    ImageView image = (ImageView) dialog.findViewById(R.id.Image); 

    // Load Image & Make Zoomable 
    PhotoViewAttacher mAttacher = new PhotoViewAttacher(image); 
    image.setImageURI(imgUri); 
    mAttacher.update(); 

    // Get animations ready 
    final Animation fiCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_in); 
    fiCloseAnim.setFillEnabled(true); 
    fiCloseAnim.setFillAfter(true); 
    final Animation foCloseAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.fade_out); 
    foCloseAnim.setFillEnabled(true); 
    foCloseAnim.setFillAfter(true); 
    final Animation dBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_down); 
    dBarAnim.setFillEnabled(true); 
    dBarAnim.setFillAfter(true); 
    final Animation uBarAnim = AnimationUtils.loadAnimation(ActivitySend.this, R.anim.slide_up); 
    uBarAnim.setFillEnabled(true); 
    uBarAnim.setFillAfter(true); 

    // Reset static variables 
    ActionsHidden = false; 
    AnimStarted = false; 
    closeAnimFinished = false; 
    barAnimFinished = false; 

    // Initialize listeners 
    AnimationListener closeAnimListener = new AnimationListener() 
    { 
     @Override 
     public void onAnimationEnd(Animation animation) 
     { 
      closeAnimFinished = true; 
      if (closeAnimFinished && barAnimFinished) 
      { 
       AnimStarted = false; 
       closeAnimFinished = false; 
       barAnimFinished = false; 

       if (ActionsHidden) 
       { 
        actions.setVisibility(View.VISIBLE); 
        btnClose.setVisibility(View.VISIBLE); 
       } 
       else 
       { 
        actions.setVisibility(View.GONE); 
        btnClose.setVisibility(View.GONE); 
       } 
       ActionsHidden = !ActionsHidden; 
      } 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) 
     { 
      // Nothing 
     } 

     @Override 
     public void onAnimationStart(Animation animation) 
     { 
      AnimStarted = true; 
     } 
    }; 
    AnimationListener barAnimListener = new AnimationListener() 
    { 
     @Override 
     public void onAnimationEnd(Animation animation) 
     { 
      barAnimFinished = true; 
      if (closeAnimFinished && barAnimFinished) 
      { 
       AnimStarted = false; 
       closeAnimFinished = false; 
       barAnimFinished = false; 

       if (ActionsHidden) 
       { 
        actions.setVisibility(View.VISIBLE); 
        btnClose.setVisibility(View.VISIBLE); 
       } 
       else 
       { 
        actions.setVisibility(View.GONE); 
        btnClose.setVisibility(View.GONE); 
       } 
       ActionsHidden = !ActionsHidden; 
      } 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) 
     { 
      // Nothing 
     } 

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

    // Set listeners 
    fiCloseAnim.setAnimationListener(closeAnimListener); 
    foCloseAnim.setAnimationListener(closeAnimListener); 
    dBarAnim.setAnimationListener(barAnimListener); 
    uBarAnim.setAnimationListener(barAnimListener); 

    // Actions Appear/Disappear onTap (Animate) 
    mAttacher.setOnPhotoTapListener(new OnPhotoTapListener() 
    { 
     @Override 
     public void onPhotoTap(View view, float x, float y) 
     { 
      if (!AnimStarted && ActionsHidden) 
      { 
       actions.startAnimation(uBarAnim); 
       btnClose.startAnimation(fiCloseAnim); 
      } 
      else if (!AnimStarted) 
      { 
       actions.startAnimation(dBarAnim); 
       btnClose.startAnimation(foCloseAnim); 
      } 
     } 
    }); 

    // Make dialog square 
    ContainerChanged = false; 
    ViewTreeObserver vto = Container.getViewTreeObserver(); 
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() 
    { 
     public boolean onPreDraw() 
     { 
      // Set boolean to save processing power 
      if (!ContainerChanged) 
      { 
       int width = Container.getMeasuredWidth(); 
       Container.getLayoutParams().height = width; 
       ContainerChanged = true; 
      } 

      return true; 
     } 
    }); 

    // Set button listeners 
    btnClose.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      dialog.dismiss(); 
     } 
    }); 
    btnReplace.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
     } 
    }); 
    btnRemove.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
     } 
    }); 

    // Show dialog 
    dialog.show(); 
} 

一切都完美地工作,如果我刪除動畫和只使用view.setVisibility(View.GONE)但我: After

我對下面的對話框代碼真的很喜歡動畫...

有什麼想法可以解決這個問題? 我大概可以使用view.setClickable(false);,但我確定有比這更好的解決方案。

回答

0

你可以這樣做:

讓你的活動實現AnimationListener。然後將有一個覆蓋的方法public void onAnimationEnd(Animation arg0),寫入setClickable(false)setEnabled(false)在這個方法中的所有三個按鈕,當你開始你的動畫時再次啓用它們....希望你讓我..

+0

您的解決方案工作,但只在一定程度上。當你點擊我想要的動畫之後,它們不再出現。但是,圖像啓用了縮放縮放功能,並且按鈕仍然擋住手勢。我不知道爲什麼... – Pkmmte

+0

哦,等等,我正在使整個按鈕欄不可點擊,而不是底部按鈕本身。這完美現在完美,謝謝! – Pkmmte

+0

歡迎,快樂編碼... – Vikram