2012-10-25 31 views
3

我正在嘗試爲我的應用程序構建啓動屏幕。當我嘗試更改我的imagesview屬性並設置動畫時,我收到一個錯誤。只有創建視圖層次結構的原始線程才能觸及其視圖

我已經測試了runonuithread和Hadler,但在這2種情況下沒有任何錯誤,屏幕上也沒有顯示任何內容,並且延遲後(正是我在代碼中指定的內容)程序活動切換到下一個活動我的代碼:

@Override 
public void run(){ 
    try { 
      TranslateAnimation moveLefttoRight = new TranslateAnimation(500, 0, 0, 0); 
      moveLefttoRight.setDuration(5000); 
      moveLefttoRight.setFillAfter(true); 
      im.startAnimation(moveLefttoRight);//im is an image view 
      SystemClock.sleep(6000); 
      TranslateAnimation moveRighttoLeft = new TranslateAnimation(0, 100, 0, 0); 
      moveRighttoLeft.setDuration(1000); 
      moveRighttoLeft.setFillAfter(true); 
      im.startAnimation(moveRighttoLeft); 
      SystemClock.sleep(6000); 
      ScaleAnimation scale = new ScaleAnimation(0.3f, 1f, 0.3f, 1f, 
        ScaleAnimation.RELATIVE_TO_SELF, 0f, 
        ScaleAnimation.RELATIVE_TO_SELF, 0f); 
      scale.setDuration(1000); 
      scale.setFillAfter(true); 
      // Wait given period of time or exit on touch 

      sig.startAnimation(scale);//sig is an image view 

      SystemClock.sleep(2000); 

    } 

我該如何解決這個奇怪的錯誤?

+0

你正試圖使鑑於UI線程的出方的變化......你必須動畫UI線程內您的ImageView不是在你的線程 –

+0

我嘗試UI線程有沒有錯誤,但只是告訴我一個黑屏屏幕inested我的圖像 –

+0

然後,您必須檢查您的代碼或發佈您的代碼尋求幫助...並始終記住,只有UI線程可以在視圖中進行更改 –

回答

0

我試過你的代碼,它沒有問題。當您更改任何視圖時,您必須在UI /主線程上執行此操作。你可以試試我編輯的代碼:

TranslateAnimation moveLefttoRight = new TranslateAnimation(500, 0, 0, 
     0); 
moveLefttoRight.setDuration(5000); 
moveLefttoRight.setFillAfter(true); 
bt1.startAnimation(moveLefttoRight);// im is an image view 

moveLefttoRight.setAnimationListener(new AnimationListener() { 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     doAnim2(); // do the next animation 
    } 
}); 
+0

坦克我的朋友它的工作我的問題解決了你的這麼多 –

相關問題