Canvas画弧

一、代码

package com.wang.ader.demoframe.widget.customView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;


/**
 * Create by Wang Xiaowu on 2019/7/22
 * Describe:
 */
public class DemoCircleView extends View {

    int mColor=0xffff0000;


    public DemoCircleView(Context context) {
        super(context);
        init(context,null);
    }

    public DemoCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public DemoCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public DemoCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs){

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        RectF ovral=new RectF(0,0,getWidth()/2,getHeight()/2);

        Paint paint1=new Paint();    //采用默认设置创建一个画笔
        paint1.setAntiAlias(true);//使用抗锯齿功能
        paint1.setColor(mColor);    //设置画笔的颜色为绿色
        paint1.setStyle(Paint.Style.FILL);//设置画笔类型为填充
        /***********绘制圆弧*************/
        RectF rectf_head1=new RectF(0, 0, dp2px(getContext(),40), dp2px(getContext(),40));//确定外切矩形范围
        rectf_head1.offset(dp2px(getContext(),0), dp2px(getContext(),40));//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
        canvas.drawArc(rectf_head1, -20, -140, false, paint1);//绘制圆弧,不含圆心

        /***********配置画笔*************/
        Paint paint2=new Paint();    //采用默认设置创建一个画笔
        paint2.setAntiAlias(true);//使用抗锯齿功能
        paint2.setColor(mColor);    //设置画笔的颜色为绿色
        paint2.setStyle(Paint.Style.FILL);//设置画笔类型为填充
        /***********绘制圆弧*************/
        RectF rectf_head2=new RectF(0, 0, dp2px(getContext(),40), dp2px(getContext(),40));//确定外切矩形范围
        rectf_head2.offset(dp2px(getContext(),50), dp2px(getContext(),40));//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
        canvas.drawArc(rectf_head2, -20, -140, true, paint2);//绘制圆弧,含圆心


        /***********配置画笔*************/
        Paint paint3=new Paint();    //采用默认设置创建一个画笔
        paint3.setAntiAlias(true);//使用抗锯齿功能
        paint3.setColor(mColor);    //设置画笔的颜色为绿色
        paint3.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE(个人感觉是描边的意思)
        paint3.setStrokeWidth(dp2px(getContext(),12));
        /***********绘制圆弧*************/
        RectF rectf_head3=new RectF(0, 0, dp2px(getContext(),40), dp2px(getContext(),40));//确定外切矩形范围
        rectf_head3.offset(dp2px(getContext(),150), dp2px(getContext(),40));//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
        canvas.drawArc(rectf_head3, -20, -140, true, paint3);//绘制圆弧,含圆心


        /***********配置画笔*************/
        Paint paint4=new Paint();    //采用默认设置创建一个画笔
        paint4.setAntiAlias(true);//使用抗锯齿功能
        paint4.setColor(mColor);    //设置画笔的颜色为绿色
        paint4.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE类型(个人感觉是描边的意思)
        paint4.setStrokeWidth(dp2px(getContext(),12));
        /***********绘制圆弧*************/
        RectF rectf_head4=new RectF(0, 0, dp2px(getContext(),dp2px(getContext(),40)), dp2px(getContext(),dp2px(getContext(),40)));//确定外切矩形范围
        rectf_head4.offset(dp2px(getContext(),200), dp2px(getContext(),40));//使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
        canvas.drawArc(rectf_head4, -20, -140, false, paint4);//绘制圆弧,不含圆心
    }

    public static int dp2px(@NonNull Context context, float dpValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
    }
}

二、效果图 

 

三、总结

函数drawArc(),是否包含圆心,第四个参数决定的(true:包含;false:不包含)。圆弧是否为填充由画笔的类型决定的,paint.setStyle()的参数决定的(Paint.Style.STROKE:描边(个人的翻译,未必准确);Paint.Style.FILL:填充;Paint.Style.FILL_AND_STROKE:既填充又描边)。

四、参考

1.android中画弧函数canvas.drawArc()之理解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值