I'm trying to create a graph using the class RectF
and the class Canvas
, for this I created a View
custom, where I draw drawc.
I also have to insert text in the center of the drawn graph, I am trying to center the text using canvas.getWidth/2
and canvas.getHeight/2
and passing those values within canvas.drawText
.
follows the class code
public class Circle extends View {
private static final int START_ANGLE = 0;
private Paint paint;
private RectF rectF;
private float angle;
private Paint paintCinza;
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(30);
paint.setColor(getResources().getColor(R.color.verde));
paint.setTextAlign(Paint.Align.CENTER);
rectF = new RectF(40,40, 250, 250);
angle = 120;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
Paint p1 = new Paint();
p1.setColor(getResources().getColor(R.color.cinza));
p1.setTextSize(80);
canvas.drawText("72%", (canvas.getWidth() ) / 2, (canvas.getHeight()) / 2, p1);
canvas.drawArc(rectF, START_ANGLE, angle, false, paint);
}
public float getAngle() {
return angle;
}
public void setAngle(float angle) {
this.angle = angle;
}
}
xml:
<br.net.getinfo.treinamentos.view.Circle
android:id="@+id/circle"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:background="@android:color/transparent" />
What do I need to change to get the graph I created to be centered within the space of Canvas
(space in the black color of the image)?