2017-01-21 12 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

私はカスタム表示クラスで試していますか?実際には見えません。私はキャンバスを持っているので、キャンバスはstartAnimationメソッドを持っていません。私のコードを見てください。 アルファアニメーションが正常に動作する方法を知っています – 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(); 

} 

頃は、私はおそらく、この仕事のためにpostDelayed()Handlerを使用しますが、これはあなたにそれを行うことができるかのような印象を与える必要があります。

関連する問題