2012-03-17 44 views
0

我使用的.java下面的代碼(主要活動)嘗試:的動畫允許一個onClick()函數

final ImageView diskView1 = (ImageView) findViewById(R.id.can); 
diskView1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v){ 

     System.out.println("Clicked."); 

    AnimationSet canMov; 
     RotateAnimation canRotate; 
    TranslateAnimation canTrans; 

      canMov = new AnimationSet(true); 
      canRotate = new RotateAnimation(0,1360, Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF,0.5f); 
      canRotate.setStartOffset(50); 
      canRotate.setDuration(20000); 
      canMov.addAnimation(canRotate); 
      canTrans = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.35f); 
      canTrans.setDuration(20000); 
      canMov.addAnimation(canTrans); 
      canMov.setRepeatCount(0); 
      canMov.setRepeatMode(Animation.REVERSE); 
      diskView1.setAnimation(canMov); 
      } 
    }); 

我能夠得到的消息在logcat的「點擊」,但動畫後不響應點擊。 但是,如果沒有使用onClick(),我會得到罐子的完整動畫。 我只需點擊罐子就可以開始動畫。 我在做什麼錯?

+0

您是否獲得在logcat的一個例外? – 2012-03-17 17:36:32

+0

調試:GC_EXPLICIT釋放了91個對象。沒有例外或錯誤。 – sumit 2012-03-17 17:40:24

回答

0

我有類似的問題(在一個片段中工作)。我創建了一個方法來將圖像視圖向右滑動。例如,如果我在onCreateView中調用該方法,它將起作用。

然後,我將一個偵聽器附加到圖像視圖(帶有setOnClickListener),並使用onClick調用動畫方法。即使動畫方法被調用,動畫也不起作用(正如我可以在我的日誌中看到的那樣)。

我發現如果我將clearAnimation()稱爲動畫所附加的圖像視圖,那麼動畫就可以工作。所以,如果我的圖像視圖,我想要動畫稱爲imageViewHouse,我會添加此作爲我的動畫方法的第一行:imageViewHouse.clearAnimation()。然後我像這樣加載動畫:pushRight = AnimationUtils.loadAnimation(context,R.anim.push_right);並使用以下命令啓動動畫:houseView.setAnimation(pushRight);

它適用於我,不知道爲什麼,但它確實(花了我幾個小時才找到它!)。希望能幫助別人。

問候

克萊夫

相關問題