2015-06-01 79 views
0

我有兩個按鈕,btn1和btn2。我想在兩個按鈕保持按下狀態的同時播放動畫,並且當按鈕中的任何一個未按下或甚至兩個按鈕都達到action_up狀態(未按下)時動畫都會停止。當兩個按鈕被按下時,使ontouchListener工作

這裏是我的代碼:

final Animation animation = AnimationUtils.loadAnimation(this, 
      R.anim.aim); 

    animation.reset(); 
    final ImageView maxName = (ImageView) findViewById(R.id.imageView1); 

    Button btn1 = (Button)findViewById(R.id.button3); 
    Button btn2 = (Button)findViewById(R.id.button2); 

    btn1.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getActionMasked(); 


      if (action == MotionEvent.ACTION_DOWN) { 



       maxName.startAnimation(animation); 



      } else if (action == MotionEvent.ACTION_UP 
        || action == MotionEvent.ACTION_CANCEL) { 



       maxName.clearAnimation(); 


      } 
      // TODO Auto-generated method stub 
      return false; 
     } 

現在我有一個按鈕,玩它,但我想按我上面寫的做出改變。謝謝。等待幫助。

回答

0
final boolean oneIsPressed = false; 
     final boolean twoIsPressed = false; 
     final boolean isAnimating = false; 
     Button btn1 = (Button) findViewById(R.id.button3); 
     Button btn2 = (Button) findViewById(R.id.button2); 

     View.OnTouchListener touchListener = new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       int action = event.getActionMasked(); 


       if (action == MotionEvent.ACTION_DOWN) { 
        if (v.getId() == R.id.button3) { 
         oneIsPressed = true; 
        } else if (v.getId() == R.id.button2) { 
         twoIsPressed = true; 
        } 

        if (oneIsPressed && twoIsPressed && !isAnimating) { 
         isAnimating = true; 
         maxName.startAnimation(animation); 
        } 
       } else if (action == MotionEvent.ACTION_UP 
         || action == MotionEvent.ACTION_CANCEL) { 
        isAnimating = false; 
        maxName.clearAnimation(); 
       } 
       return false; 
      } 
     }; 

     btn1.setOnTouchListener(touchListener); 
     btn2.setOnTouchListener(touchListener); 

    } 
+0

工作正常。但是一個小問題。一旦動畫播放和按鈕不變,現在再次如果我嘗試通過按他們兩個播放動畫,動畫不播放。 –

+0

添加isAnimating = false;在「Action_UP」裏面。我編輯了答案。 – roiberg

+0

解決了動畫問題。但仍然是代碼中留下的小故障。只有在一個實例按下兩個按鈕時,動畫才應播放。它的確如此工作,但我所討論的小故障是,當按下一個按鈕時,動畫不會播放= FINE。我離開第一個按鈕,然後按第二個按鈕動畫開始播放= NOT FINE。 –

0

添加一個全局布爾變量,當單擊一個按鈕時設置爲true。例如:

private boolean isButtonOneClicked; 

@Override 
     public boolean onTouch(View v, MotionEvent event) { 
      int action = event.getActionMasked(); 


      if (action == MotionEvent.ACTION_DOWN) { 
       isButtonOneClicked = true; 
      } else if (action == MotionEvent.ACTION_UP 
        || action == MotionEvent.ACTION_CANCEL) { 
       isButtonOneClicked = false; 
      } 

添加onTouchListener第二個按鈕,併爲這個按鈕的全局變量,以及,完全一樣的第一個按鈕,它只是有另外一個名字。

創建,檢查兩個布爾值是否真實的方法,然後開始動畫

public void startAnimation() { 
    if(isButtonOneClicked && isButtonTwoClicked) { 
     maxName.startAnimation(animation); 
    } else { 
     maxName.clearAnimation(); 
    } 
} 

每當一個按鈕被點擊或者未點擊現在調用此方法。

相關問題