drawArc支持画圆、椭圆、弧段,用案例自己试试理解各个参数含义功能比较好!
**MyView**
package com.bawei.zdy_view_yuan;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private Paint paint;
private float cx = 100f;
private float cy = 100f;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//画笔
paint = new Paint();
//设置颜色
paint.setColor(Color.parseColor("#ff0000"));
//设置抗锯齿
paint.setAntiAlias(true);
//设置画笔粗细
paint.setStrokeWidth(2);
//设置是否为空心
paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.STROKE);
RectF oval=new RectF(); //RectF对象
oval.left=0; //左边
oval.top=0; //上边
oval.right=500; //右边
oval.bottom=500; //下边
canvas.drawArc(oval, 90, 270, true, paint); //绘制圆弧
@Override
public boolean onTouchEvent(MotionEvent event) {
cx = event.getX();
cy = event.getY();
//刷新view
invalidate();
return true;
}
}
*activity_main*
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bawei.zdy_view_yuan.MainActivity">
<com.bawei.zdy_view_yuan.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>