利用Android畫圓弧canvas.drawArc()實例詳解
前言
在學(xué)習(xí)android中圖形圖像處理技術(shù)這部分內(nèi)容時,對繪制圓弧函數(shù)canvas.drawArc()的用法、參數(shù)含義及畫圖原理很是不理解,在網(wǎng)上搜索了一些,加上自己的理解,在此做個小總結(jié),下面來一起看看吧。
示例代碼
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,
@NonNull Paint paint) {
drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter,
paint);
}
要實現(xiàn)這個方法,我們要傳5個參數(shù)進去。
第一個參數(shù):RectF oval
oval 參數(shù)的作用是:定義的圓弧的形狀和大小的范圍
/** * 這是一個居中的圓 */ float x = (getWidth() - getHeight() / 2) / 2; float y = getHeight() / 4; RectF oval = new RectF( x, y, getWidth() - x, getHeight() - y);
第二個參數(shù):float startAngle
這個參數(shù)的作用是設(shè)置圓弧是從哪個角度來順時針繪畫的
canvas.drawArc(oval,-90,120,false,mPaint);

canvas.drawArc(oval,90,110,false,mPaint);

//設(shè)置為-180的時候也是這樣 canvas.drawArc(oval,180,140,false,mPaint);

//設(shè)置為360的時候也是這樣 canvas.drawArc(oval,0,140,false,mPaint);

第三個參數(shù):float sweepAngle
這個參數(shù)的作用是設(shè)置圓弧掃過的角度
我們從上面的代碼就可以知道其中的作用了
第四個參數(shù):boolean useCenter
這個參數(shù)的作用是設(shè)置我們的圓弧在繪畫的時候,是否經(jīng)過圓形
值得注意的是,這個參數(shù)在我們的 mPaint.setStyle(Paint.Style.STROKE); 設(shè)置為描邊屬性的時候,是看不出效果的。
/**
*這里我是偷懶了,建議不要在onDraw()方法里初始化對象
*/
Paint p = new Paint();//這個是畫矩形的畫筆,方便大家理解這個圓弧
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.RED);
mPaint.setAntiAlias(true);//取消鋸齒
mPaint.setStyle(Paint.Style.FILL);//設(shè)置畫圓弧的畫筆的屬性為描邊(空心),個人喜歡叫它描邊,叫空心有點會引起歧義
mPaint.setStrokeWidth(mCircleWidth);
mPaint.setColor(Color.CYAN);
/**
* 這是一個居中的圓
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
canvas.drawArc(oval,360,140,false,mPaint);//畫圓弧,這個時候,繪制沒有經(jīng)過圓心
canvas.drawRect(oval, p);//畫矩形

//當(dāng)我們設(shè)置為true的時候,繪制的時候就經(jīng)過圓心了 canvas.drawArc(oval,360,140,true,mPaint);

第五個參數(shù):Paint paint
這個參數(shù)的作用是設(shè)置我們的畫筆對象的屬性
//當(dāng)我們設(shè)置為true的時候,繪制的時候就經(jīng)過圓心了 canvas.drawArc(oval,360,140,true,mPaint);
這里還是要強調(diào)一下,當(dāng) p.setStyle(Paint.Style.STROKE)的時候,我們的第四個參數(shù)boolean useCenter ,是看不到效果的。
下面是代碼全文
public class CustomProgress extends View{
private Paint mPaint;
/**
* 圓的寬度
*/
private int mCircleWidth = 3;
public CustomProgress(Context context) {
this(context, null);
}
public CustomProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setAntiAlias(true);//取消鋸齒
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(mCircleWidth);
mPaint.setColor(Color.CYAN);
/**
* 這是一個居中的圓
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
canvas.drawArc(oval,360,140,true,mPaint);
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
android開發(fā)教程之使用looper處理消息隊列
這篇文章主要介紹了通過HandlerThread對象來實現(xiàn)使用looper處理消息隊列的功能,大家參考使用吧2014-01-01
Android實現(xiàn)Activity水平和垂直滾動條的方法
這篇文章主要介紹了Android實現(xiàn)Activity水平和垂直滾動條的方法,涉及Activity的ScrollView設(shè)置相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android ApplicationInfo 應(yīng)用程序信息的詳解
這篇文章主要介紹了Android ApplicationInfo 應(yīng)用程序信息的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android PopWindow 設(shè)置背景亮度的實例
這篇文章主要介紹了Android PopWindow 設(shè)置背景亮度的實例的相關(guān)資料,這里提供實現(xiàn)方法,希望能幫助有所需要的朋友,需要的朋友可以參考下2017-08-08
Android BottomNavigationBar底部導(dǎo)航控制器使用方法詳解
這篇文章主要為大家詳細介紹了Android BottomNavigationBar底部導(dǎo)航控制器使用方法,感興趣的小伙伴們可以參考一下2016-03-03
Android協(xié)程作用域與序列發(fā)生器限制介紹梳理
協(xié)程的作用是什么?協(xié)程是一種輕量級的線程,解決異步編程的復(fù)雜性,異步的代碼使用協(xié)程可以用順序進行表達,文中通過示例代碼介紹詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-08-08
Android自定義ViewGroup實現(xiàn)可滾動的橫向布局(2)
這篇文章主要介紹了Android自定義ViewGroup實現(xiàn)可滾動的橫向布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android實現(xiàn)濾鏡效果ColorMatrix
這篇文章主要為大家詳細介紹了Android實現(xiàn)濾鏡效果ColorMatrix,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05

