2012-04-02 144 views
1

我畫使用onDraw(canvas)弧:動態繪製弧形

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(new MyView(this)); 
} 

public class MyView extends View { 

    public MyView(Context context) { 
     super(context); 
    } 

    @Override 
    public void onDraw(Сanvas canvas) { 
     super.onDraw(canvas); 
     float width = (float) getWidth(); 
     float height = (float) getHeight(); 
     float radius; 
     if (width > height) { 
      radius = height/4; 
     } else { 
      radius = width/4; 
     } 

     final Paint paint = new Paint(); 
     paint.setColor(Color.WHITE); 
     paint.setStrokeWidth(50); 
     paint.setStyle(Paint.Style.STROKE); 

     float center_x, center_y; 
     center_x = width/2; 
     center_y = height/4; 
     final RectF oval = new RectF(); 
     oval.set(center_x - radius, center_y - radius, center_x + radius, 
       center_y + radius); 

     paint.setStyle(Paint.Style.STROKE); 
     center_x = width/2; 
     center_y = height * 3/4; 
     oval.set(center_x - radius, center_y - radius, center_x + radius, 
       center_y + radius); 

     canvas.drawArc(oval, -90, 45, false, paint); 
    } 
} 

告訴我,如何動態地改變行 canvas.drawArc(oval, -90, 45, false, paint)sweepAngle() == 45的值?

回答

1

一種解決方案是在你的班級中有一個sweepAngle字段,並在繪製圓弧時使用該字段而不是45。然後有一個定時器,定期添加到sweepAngle並重畫畫布。

+0

謝謝你的幫助。 – monomi 2012-04-02 13:42:34