动画onDraw()部分

时间:2011-05-30 19:18:51

标签: android animation ondraw

我有以下课程:

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()

}


 }

所以基本上这基于弧方程一次一个地制作圆形图像。我想要显示一个动画,就像所有正在填充的圆圈一样(所以显示每个剪切的路径,演示正在构建的圆圈)。所以我想做一个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时绘制最终结果,任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

目前,您的循环是在主线程上执行并使其保持忙碌,因此在完成之前它不会实际更新UI。

您应该在后台线程中执行循环(使用TimerAsyncTask)并在主线程上执行绘制。请注意,如果没有短暂的睡眠,它可能会看起来像动画太多,它会非常快。