Android(안드로이드) drawArc 를 사용하여 원을 그리는 방법
drawArc 를 사용하여 원을 그리는 방법
[xml code]
< RelativeLayout
android:id=”@+id/chart_area”
android:layout_width=”180dp”
android:layout_height=”180dp”
android:layout_below=”@+id/main_info”
android:layout_marginTop=”10dp”
android:layout_centerHorizontal=”true”
android:gravity=”center|center_vertical”
android:background=”@drawable/main_circle_backgroud”>
< TextView
android:id=”@+id/main_mission_rate”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”45dp”
android:textSize=”40sp”
android:textColor=”@color/welcolor27″
/>
< TextView
android:id=”@+id/main_point”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_below=”@+id/main_mission_rate”
android:textSize=”18sp”
android:textColor=”@color/welcolor28″
/>
< /RelativeLayout>
private RelativeLayout chartArea;
private RingCanvas circleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chartArea = (RelativeLayout) findViewById (R.id.chart_area);
}
//화면 해상도
DisplayMetrics dm = getResources().getDisplayMetrics();
int displayWidth = (int) (200 * dm.density);
//DP값으로 변경
int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int circleSize = displayWidth / 2 + 200;
params.width = circleSize;
params.height = circleSize;
params.addRule(RelativeLayout.BELOW, R.id.main_info);
params.setMargins(0,marginTop,0,0);
params.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); //가운데 정렬
chartArea.setLayoutParams(params);
circleView = new RingCanvas(this);
circleView.setStartAngle(90); //acr가 그려질 각도 defalut 경우 시계 3시방향(90도) 기준
circleView.setGraphSize(circleSize);
circleView.setRingWidth(50);
circleView.setGraghColor(getResources().getColor(R.color.welcolor27));
chartArea.addView(circleView);
circleView.setSweepAngle(getRatingAngle(2, 3));
[getRatingAngle]
private int getRatingAngle(int perform, int total) {
int rating = 0;
if(perform != 0 && total != 0) {
rating = (perform * 360) / total;
}
return rating;
// return 360;
}
[RingCanvas] 클래스
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
public class RingCanvas extends View {
private static final float SWEEP_INTERVAL = 10f;
private Paint paint;
private RectF rectF;
private float increaseValue = 0;
private float startAngle = 270f;
private float backgroundAngle = 360f;
private int graphColor = 0xffc3e32f;
private int graphGab = 16;
private int graphSize = 0;
private float ringWidth = 16f;
private int angle;
public RingCanvas (Context context) {
super(context);
rectF = new RectF();
}
public void setRingWidth(int graghWidth) {
this.ringWidth = graghWidth;
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(ringWidth);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.BUTT);
}
public void setStartAngle(int angle) {
startAngle = angle;
}
public void setBackgoundAngle(int angle) {
backgroundAngle = angle;
}
public void setGraghColor(int color) {
graphColor = color;
}
public void setGraphGab(int gab) {
graphGab = gab;
}
public void setGraphSize(int size) {
graphSize = size;
}
public void setSweepAngle(int angle){
this.angle = angle;
increaseValue = 0;
invalidate();
}
private void onDrawGragh(Canvas canvas, RectF oval, boolean useCenter, Paint paint) {
if(angle < 180)
paint.setColor(0xffff0000);
else
paint.setColor(graphColor);
canvas.drawArc(oval, startAngle, increaseValue, useCenter, paint);
}
@Override
protected void onDraw(Canvas canvas) {
//int width = canvas.getWidth();
//int height = canvas.getHeight();
//RectF oval = new RectF(0, 0, w, h); // 원그리는 클래스
rectF.left = ringWidth /2;
rectF.top = ringWidth /2;
rectF.right = graphSize – ringWidth/2;
rectF.bottom = graphSize- ringWidth/2;
onDrawGragh(canvas, rectF, false, paint);
if(increaseValue < angle) {
increaseValue += SWEEP_INTERVAL;
invalidate();
}
}
}
레퍼런스
drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint pnt)
심플방법
Paint pnt = new Paint();
pnt.setStrokeWidth(5f);
pnt.setColor(Color.parseColor(“#FF0000”));
pnt.setStyle(Paint.Style.STROKE);
RectF rect = new RectF();
rect.set(300, 300, 400, 400);
canvas.drawArc(rect, (270), 290, false, pnt);
rect = new RectF();
rect.set(200, 600, 600, 1000);
canvas.drawArc(rect, 0, 290, true, pnt);