Paint:畫筆類,用於設置顏色和樣式.
Paint.Style
-
Paint.Style.FILL設置只繪製圖形內容
-
Paint.Style.STROKE設置只繪製圖形的邊
-
Paint.Style.FILL_AND_STROKE設置都繪製
Shader
Shader is the based class for objects that return horizontal spans of colors during drawing. A subclass of Shader is installed in a Paint calling paint.setShader(shader). After that any object (other than a bitmap) that is drawn with that paint will get its color(s) from the shader.
Shader(着色器)是繪製時水平方向顏色對象的基類。它的子類將作爲paint.setShader(shader)方法的參數。
當調用了paint.setShader(shader)之後,除了任何對象(除了bitmap)在用那個paint繪製時,都會從shader(着色器)中獲得它的顏色
SweepGradient(掃描式漸變)
繼承自Shader類.
//構造函數
public SweepGradient(float cx, float cy, @ColorInt int color0, @ColorInt int color1) {}
- 構造1個圍繞中心點繪製掃描漸變的着色器。
- 參數cx,cy爲中心點的座標,後2個參數分別爲掃描的起點與終點的顏色
Canvas(翻譯爲畫布)
1.Canvas類的官方介紹:
The Canvas class holds the “draw” calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
- Canvas類有很多以draw開頭的方法,也就是他可以畫各種各樣的圖形
- 要draw一個東西,需要4個基本的組件:
- 要有1個Bitmap來保存像素
- 1個canvas對象來調用draw方法,寫入到上面的bitmap中
- 要畫的東西,比如矩形,路徑,文本,Bitmap等
- 最後還有1個Paint對象,用於描述顏色和樣式風格
2.Canvas可以畫哪些?
弧線(arcs)、填充顏色(argb和color)、 Bitmap、圓(circle和oval)、點(point)、線(line)、矩形(Rect)、圖片(Picture)、圓角矩形 (RoundRect)、文本(text)、頂點(Vertices)、路徑(path)
2.1 drawArc()
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,@NonNull Paint paint )
第一個參數:RectF類(以浮點座標表示的矩形)
RectF mRectF = new Rect();
/*
* left和top爲矩形左上頂點的x和y座標
* right和bottom爲矩形右下頂點的x座標和y座標
* 注意,原點是當前View的左上角頂點,而不一定是整個屏幕的左上角
*/
public void set(float left, float top, float right, float bottom) {
...
}
後面的參數
-
oval: 橢圓的邊界,用於定義圓弧的形狀和大小
-
startAngle : 弧的起始角度(時鐘3點方向爲0°,順時針方向開始畫)
-
sweepAngle:掃過的角度
-
useCenter:true表示會與矩形的中心點連接成爲扇形,否則不會。這
-
paint: 畫筆。如果Paint的Style爲Stroke則沒有扇形效果,只有一條弧線。
舉個栗子:
canvas.drawArc(oval,-90,120,false,mPaint);
設置Paint的Style爲Stroke的效果
設置Paint的Style爲FILL的效果
設置useCenter設置爲true的效果:
注:sweepAngle >= 360時,效果都是畫1個圓。
3.Canvas save() 和 restore()(保存和還原)
save()
- 保存的是座標系的原點,座標軸的方向
- 隨後的平移,縮放,旋轉,傾斜,concat或clipRect,clipPath調用都將照常運行,當後面調用restore()後,將會恢復到上一次save()時的狀態,save()和restore()中間的調用將會被忘記
restore()
- 恢復到上一次save()時的狀態,resotre()調用次數比調用save()多時,將會發生錯誤
舉個栗子:
//保存當前狀態
canvas.save();
//選擇座標系
canvas.rotate(mSweep, centerX, centerY);
//畫圓弧
canvas.drawArc(mRectF, 0, mSweep, true, mArcPaint);
//恢復到上次保存的狀態
canvas.restore();
4.Canvas旋轉
public void rotate(float degrees) {...}
- 將座標系選擇指定角度,默認是View的左上角頂點水平正向爲x軸正向,垂直下方向爲y軸正向
public final void rotate(float degrees, float px, float py){...}
- 以(px,py)爲座標系中心,選擇指定角度