2011-05-16 69 views
2

也許簡單的問題,但找不到解決方案。如何解決問題雖然更新/刷新 - 顯示動畫?

我有刷新/更新一些內容的按鈕。稱爲「刷新」的按鈕。 在這個按鈕的背景 - 我有圖像。我想在更新內容的同時爲此圖像設置旋轉的動畫。但我有一些問題:第一 - 我的內容更新,只有比我可以看到動畫:這是我的代碼:

Timer myTimer; 
    public void timerS() 
    { 
     myTimer = new Timer(); 
     myTimer.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       TimerMethod(); 
      } 
     }, 0, 100); 
    } 
    private void TimerMethod() 
    { 
     this.runOnUiThread(Timer_Tick); 
    } 

    private Runnable Timer_Tick = new Runnable() { 
     public void run() { 
     // drawMatrix(); 
      animation(); 
      seconds++; 
     } 

    }; 
    int degree =60; 
    int oneClick=1; 
    public int nowUpdated = 0; 
     public void animation() 
     { 
     int currentRotation = 0; 
      RotateAnimation anim = new RotateAnimation(currentRotation, (360*4), 
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f); 
      currentRotation = (currentRotation + 45) % 360; 

      anim.setInterpolator(new LinearInterpolator()); 
      anim.setDuration(10000); 
      anim.setFillEnabled(true); 

      anim.setFillAfter(true); 
      refresh.startAnimation(anim); 
     } 

我試圖用這個計時器。首先我的例子是drawMatrix - 但是我不能修復圖像大小的問題。當我的圖像旋轉時 - 動畫非常大。圖像大小32x32 ..但點擊更新時 - 我認爲圖像的大小是64x64。並且無法修復該 這裏代碼抽獎矩陣:

public void drawMatrix() 
    { 
     if(nowUpdated==1) 
     { 
     Matrix mat = new Matrix(); 
     degree=degree+45; 
     mat.postRotate(degree); 
     Bitmap source = BitmapFactory.decodeResource(getResources(),R.drawable.refresh); 

     Bitmap a = Bitmap.createBitmap(source, 0, 0, 32 , 32, mat, true); 
     Drawable d =new BitmapDrawable(a); 
     refresh.setBackgroundDrawable(d); 
     } 
    } 

請,如果有人能幫助我 - 請告訴我如何改正我的代碼或第二。 問候彼得。

回答

1

在聊天中討論:使用AsyncTask在doInBackground()中執行更新處理,並在開始實際處理之前調用publishProgress() - 這將調用AsyncTask方法onProgressUpdate(您必須覆蓋),並開始你的動畫:) - 後一步是必要的,因爲onProgressUpdate可以做UI線程的東西 - doInBackground不能(因爲它不在UI線程上運行)。

乾杯,

Ready4Android

+0

yeeeeeep:d(4more) – Peter 2011-05-16 22:49:50

+0

Ready4Android,您可以登錄到scype? – Peter 2011-08-18 08:36:43

+0

嘿..你能寫信給我嗎? – Peter 2011-12-28 14:40:40