2013-05-07 19 views
7

我是一個noob android開發,我有問題使視圖無效。我正在使用this教程,並沒有實現它的問題。但是,當我更改視圖的背景時,它仍然會響應,就好像前一個背景仍然設置一樣。換句話說,我改變了面具,但我的「touchview」類沒有看到新的面具。我沒有運氣使用invalidate更新視圖,我已經驗證了mask實際上被重置爲背景。任何幫助將不勝感激。爲什麼自定義視圖不會失效?

我的代碼

@Override 
public boolean onMenuItemClick(com.actionbarsherlock.view.MenuItem item) { 
    // TODO Auto-generated method stub 
    switch (item.getItemId()) 
    { 
     case 1: // id from the xml file 
      if(isMale){ 
       isMale=false; 
       item.setIcon(R.drawable.male_icon); 
       imageViewOriginal.setImageResource(R.drawable.woman_front); 
       imageViewFlip.setImageResource(R.drawable.woman_back); 
       if(isFrontView){ 
        myMask.setBackgroundResource(R.drawable.woman_front_mask); //Mask changed here 
       }else{ 
        myMask.setBackgroundResource(R.drawable.woman_back_mask); //Mask changed here 
       } 
      }else{ 
       isMale=true; 
       item.setIcon(R.drawable.female_icon); 
       imageViewOriginal.setImageResource(R.drawable.man_front);     
       imageViewFlip.setImageResource(R.drawable.man_back); 
       if(isFrontView){ 
        myMask.setBackgroundResource(R.drawable.man_front_mask); //Mask changed here 
       }else{ 
        myMask.setBackgroundResource(R.drawable.man_back_mask); //Mask changed here 
       } 

      } 

      touchView.invalidate(); 
      infoView.invalidate(); 
      myMask.invalidate(); //Mask View Invalidated here 

      return true; // we handled the click, dont pass it up the chain 

     case 2: // id from the xml file 
      if(isFrontView){ 
       isFrontView=false; 
       if(isMale){ 
        myMask.setBackgroundResource(R.drawable.man_back_mask); //Mask changed here 
       }else{ 
        myMask.setBackgroundResource(R.drawable.woman_back_mask); //Mask changed here 
       } 
      }else{ 
       isFrontView=true; 
       if(isMale){ 
        myMask.setBackgroundResource(R.drawable.man_front_mask); //Mask changed here 
       }else{ 
        myMask.setBackgroundResource(R.drawable.woman_front_mask); //Mask changed here 
       } 
      } 
      FlipAnimator animator = new FlipAnimator(imageViewOriginal, imageViewFlip, 
        imageViewFlip.getWidth()/2, imageViewFlip.getHeight()/2); 
      if (imageViewOriginal.getVisibility() == View.GONE) { 
       animator.reverse(); 
      } 
      flipLayout.startAnimation(animator); 

      touchView.invalidate(); 
      infoView.invalidate(); 
      myMask.invalidate(); //Mask View Invalidated here 

      return true; 
    } 
    return false; 
} 
+1

爲什麼不在case case語句中使用R.id.itemId,這更直接。你真的確定你的代碼被調用嗎?並且鏈接已損壞,請在此處發佈您的代碼。 – VinceStyling 2013-05-13 02:14:43

+0

Action Bar Sherlock讓我用int id創建菜單項。上面的鏈接工作和代碼被調用。 – 2013-05-13 18:27:32

回答

22

我能想到的兩種可能性:

選項1:您正在從非UI線程上運行代碼。在這種情況下,使用postInvalidate()而不是無效()

postInvalidate():導致一個無效通過事件循環在隨後的週期 發生。使用它可以使非線程的視圖無效。

選項2:您正在從UI線程運行您的代碼。在這種情況下,我需要你發佈更多的代碼。請記住invalidate()是異步的,因爲它只調度主線程事件隊列中的重繪。這意味着只有在當前代碼全部執行完成後才能執行重繪。

在這種情況下,如果有東西阻塞了你的UI線程,你可以使用AsyncTask或Runnable來執行你的任務。

相關問題