Monday, August 29, 2011

path.addArc()

path.addArc()


package com.exercise.AndroidDraw;


import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;

public class AndroidDrawActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}

public class MyView extends View {

public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);


float width = (float)getWidth();
float height = (float)getHeight();
float radius;

if (width > height){
radius = height/4;
}else{
radius = width/4;
}

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

Path path = new Path();

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


center_x = width/2;
center_y = height/2;
oval.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
path.addArc(oval, -90, 180);

center_x = width * 3/4;
center_y = height * 3/4;
oval.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
path.addArc(oval, 0, 360);

canvas.drawPath(path, paint);

}

}
}




1 comment:

Maxood said...

Beautiful piece of code! Have you looked into my tutorial: Source Code To Display Battery Info & Chart? FYI:http://maxood-android-corner.blogspot.com/2011/03/source-code-to-display-battery-info.html