2012-04-02 3 views
1

ich einen Bogen zeichnen mit onDraw(canvas):Dynamisch Bogen zeichnen

@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); 
    } 
} 

Sag mir, wie dynamisch den Wert von sweepAngle() == 45 in Linie ändern canvas.drawArc(oval, -90, 45, false, paint)?

Antwort

1

Eine Lösung wäre, ein sweepAngle Feld in Ihrer Klasse zu haben, und verwenden Sie das anstelle von 45 beim Zeichnen des Bogens. Dann haben Sie einen Timer, der periodisch zu sweepAngle hinzufügt und die Zeichenfläche neu zeichnet.

+0

Vielen Dank für Ihre Hilfe. – monomi