2017-01-21 78 views
0

我有相當容易上課的圈子。我給參數,視圖計算其餘。我希望在畫到畫布時給予每個延遲和淡化效果。我回顧了一些關於動畫師和處理程序的文章,但我無法弄清楚。請給我看一些實現。謝謝。Android自定義視圖淡入淡出/延遲動畫

@Override 
protected void onDraw(final Canvas canvas) { 
    super.onDraw(canvas); 
    int w = getWidth(); 
    int pl = getPaddingLeft(); 
    int pr = getPaddingRight(); 
    int totalWidth = w - (pl + pr); 
    major = totalWidth/circleCount; 
    radius = major/2; 
    startPoint = totalWidth/(circleCount * 2); 
    for (int i = 0; i < circleCount; i++) { 
     canvas.drawCircle(startPoint + major * i, radius, radius, paint); 
    } 
} 

enter image description here

回答

0

這裏有一個按鈕視圖的一個簡單的奧飛動漫[它使得按鈕閃爍(這不是那麼難; O)):

import android.view.animation.AlphaAnimation; 
import android.view.animation.Animation; 
import android.view.animation.LinearInterpolator; 

final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible 
animation.setDuration(500); // duration - half a second 
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate 
animation.setRepeatCount(Animation.INFINITE);  // Repeat animation infinitely 
animation.setRepeatMode(Animation.REVERSE);   // Reverse animation at the end so the button will fade back in 

final Button btn = (Button) findViewById(R.id.but4);//replace this with your view 
btn.startAnimation(animation); 
+0

我想在自定義視圖類?其實沒有看法。我剛剛畫布,所以畫布沒有啓動動畫方法。請看我的代碼。 我知道alpha動畫是如何在正常情況下工作的 – user3792834

0

您可以使用setAlpha(int a)方法來自Paint類。 它應該工作,當你在一個單獨的線程上做一點點時間延遲在一個循環中,你從255倒數到0。

這裏是一個代碼示例,我在幾年前的早期版本的Android :

private final int FADE_TIME = 10; // modify to your needs 

private void fade() { 

    new Thread(new Runnable() { 
     @Override 
     public void run() { 

      try { 
       int i = 255; 

       while (i >= 0) { 

        paint.setAlpha(i); 
        Thread.sleep(FADE_TIME); 
        i--; 
       } 


      } catch (InterruptedException e) { 
       // do something in case of interruption 
      } 
     } 
    }).start(); 

} 

現在我可能會使用一個HandlerpostDelayed()這份工作,但是這應該給你它是如何做到的印象。