0

私は次のクラスを持っています:

public class MainActivity extends Activity {
/** Called when the activity is first created. */

String value = "0";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(new GaugeAnimation(this));

       }

     }

私の GuageAnimation クラスは次のとおりです。

public class GaugeAnimation extends View{

private Path p;
private Paint cPaint = new Paint();
private int width = 200;
private int angleStart = 135;
private int sweep = 90;
private int value=0;

Bitmap bottom = BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_bottom);
Bitmap top= BitmapFactory.decodeResource(getResources(), R.drawable.dashboard_rpm_active);

public GaugeAnimation(Context context){
    super (context);

        //Arc Equations & etc.... 

}

@Override
public void onDraw(Canvas c){


    Paint paint = new Paint();
    paint.setFilterBitmap(false);
    paint.setColor(Color.BLUE);
    c.translate(55,320);       

    //Draw bottom image on canvas

  c.save();

    p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);


    c.clipPath(p);

    //draw Image on top

    c.restore();

   invalidate()

}


 }

したがって、基本的に、これは弧の方程式に基づいて円形の画像を一度に 1 つずつトリミングします。円のピースがすべて塗りつぶされているようなアニメーションを表示したい(円が構築されていることを示す、クリップされた各パスを表示する)。だから私は次のような for ループを行うことを考えていました:

 for (int i=0; i<100; i++) {
   sweep=i;
   p.addArc(new RectF(0,0,width,width), angleStart, sweep);
    p.lineTo(width/2, width/2);

    c.clipPath(p);
        invalidate();
 }

しかし、これは機能しません.i = 100のときに最終結果を描画するだけです。これを行う方法について誰か考えがありますか?

4

1 に答える 1

1

現在、ループはメイン スレッドで実行され、ビジー状態に保たれているため、終了するまで実際には UI は更新されません。

バックグラウンド スレッドで ( TimerまたはAsyncTaskを使用して) ループを実行し、メイン スレッドでペイントを実行する必要があります。短いスリープがないと、アニメーションのように見えすぎて、かなり高速になることに注意してください。

于 2011-05-30T19:40:49.197 に答える