onMeasure:用於測量視圖的大小;java
onLayout:用於給視圖進行佈局;android
onDraw:用於對視圖進行繪製;canvas
相應的思惟導圖以下:bash
setARGB(int a, int r, int g, int b) 用於設置顏色,各參數值均爲0~255之間的整數,分別用於表示透明度、紅色、綠色和藍色值app
setColor(int color) 用於設置顏色,參數color能夠經過Color類提供的顏色常量指定 ide
3.Color.rgb(in t red,int green,int blue)方法指定顏色函數
setAlpha(int a) 用於設置透明度,值爲0~255之間的整數工具
setAntiAlias(boolean aa) 用於指定是否使用抗鋸齒功能,若是使用會使繪圖速度變慢 ,可是通常圖像繪製都會設置使用。 佈局
6.setDither(boolean dither) 用於指定是否使用圖像抖動處理,若是使用會使圖像顏色更加平滑和飽滿,更加清晰ui
setShader(Shader shader) 用於設置漸變,可使用LinearGradient(線性漸變)、RadialGradient(徑向漸變)或 者SweepGradient(角度漸變),後面分別作詳細介紹 8.setStrokeWidth(float width) 用於設置筆觸的寬度
9. setStyle(Paint.Style style) 用於設置填充風格,參數值 爲Style.FILL表示實心、Style.FILL_AND_STROKE或Style.STROKE表示空心
10.setTextAlign(Paint.Align align) 用於設置繪製文本時的文字對齊方式,參數值爲Align.CENTER、Align.LEFT或Align.RIGHT
setTextSize(float textSize) 用於設置繪製文本時的文字的大小
經過該類提供的方法,能夠繪製各類圖形,如,矩形,圓形,和線條等,一般狀況下,要在 Andaroid中繪圖,須要先建立一個繼承自View類的視圖,而且在該類中重寫它的 onDraw(Canvas canvas)方法,而後在顯示繪圖的Activity中添加該視圖
1.填充
drawARGB(int a, int r, int g, int b)
drawColor(int color)
drawRGB(int r, int g, int b)
drawColor(int color, PorterDuff.Mode mode)
2.幾何圖形
canvas.drawArc (扇形)
canvas.drawCircle(圓)
canvas.drawOval(橢圓)
canvas.drawLine(線)
canvas.drawPoint(點)
canvas.drawRect(矩形)
canvas.drawRoundRect(圓角矩形)
canvas.drawVertices(頂點)
cnavas.drawPath(路徑)
3.圖片
canvas.drawBitmap (位圖)
canvas.drawPicture (圖片)
4.文本
canvas.drawText
自定義屬性格式和意義
1. reference 引用
2. color 顏色
3. boolean 布爾值
4. dimension 尺寸值
5. float 浮點值
6. integer 整型值
7. string 字符串
8. enum 枚舉值
Paint的基本設置函數:
一、void setStyle (Paint.Style style) 設置填充樣式
Paint.Style.FILL :填充內部
Paint.Style.FILL_AND_STROKE :填充內部和描邊
Paint.Style.STROKE :僅描邊
MyView.java複製代碼
1import android.content.Context;
2import android.graphics.Canvas;
3import android.graphics.Color;
4import android.graphics.Paint;
5import android.view.View;
6
7/**
8 * Created by hhsm on 2018/8/18.
9 */
10
11public class MyView extends View {
12 Context context1;
13 public MyView(Context context) {
14 super(context);
15 context1 = context;
16 }
17 //重寫onDraw()函數,每次重繪自主實現畫圖
18 @Override
19 protected void onDraw(Canvas canvas){
20 super.onDraw(canvas);
21
22 //設置畫筆基本屬性
23 Paint paint = new Paint();
24 paint.setAntiAlias(true);//抗鋸齒功能
25 paint.setColor(Color.BLUE);//設置畫筆顏色
26 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
27 paint.setStrokeWidth(6);//設置畫筆寬度
28 paint.setShadowLayer(10,15,15,Color.GREEN);//設置陰影
29
30 canvas.drawRGB(255,255,255);//設置畫布背景顏色
31
32 canvas.drawCircle(200,200,150,paint);//畫圖
複製代碼
MainActivity.java複製代碼
1import android.app.Activity;
2import android.content.Context;
3import android.graphics.Canvas;
4import android.graphics.Color;
5import android.graphics.Paint;
6import android.graphics.Path;
7import android.graphics.RectF;
8import android.os.Bundle;
9import android.provider.DocumentsContract;
10import android.text.SpannableString;
11import android.text.style.TypefaceSpan;
12import android.util.AttributeSet;
13import android.view.View;
14import android.view.animation.Animation;
15import android.view.animation.AnimationUtils;
16import android.widget.Button;
17import android.widget.FrameLayout;
18import android.widget.ImageView;
19import android.widget.TextView;
20import android.widget.Toast;
21
22import java.util.ArrayList;
23import java.util.List;
24
25public class MainActivity extends Activity{
26
27 @Override
28 protected void onCreate(Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
30
31 setContentView(R.layout.activity_main);
32
33 FrameLayout frameLayout =(FrameLayout)findViewById(R.id.MyView);
34 frameLayout.addView(new MyView(MainActivity.this));
35 }
36}
複製代碼
activity_main.xml
注:給根結點FrameLayout添加ID號,後面用來在它的內部添加視圖用的
1<?xml version="1.0" encoding="utf-8"?>
2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:id="@+id/MyView"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:orientation="vertical"
8 tools:context=".MainActivity">
9 </FrameLayout>複製代碼
void drawLine (float startX, float startY, float stopX, float stopY, Paint paint)
startX:開始點X座標
startY:開始點Y座標
stopX:結束點X座標
stopY:結束點Y座標
1 /設置畫筆基本屬性
2 Paint paint = new Paint();
3 paint.setColor(Color.BLUE);//設置畫筆顏色
4 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
5 paint.setStrokeWidth(6);//設置畫筆寬度
6
7 canvas.drawLine(100,150,300,300,paint);
複製代碼
多條直線
void drawLines (float[] pts, Paint paint)
void drawLines (float[] pts, int offset, int count, Paint paint)
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(6);//設置畫筆寬度
7
8 float [] floats ={10,10,100,100,300,300,400,400};
9 canvas.drawLines(floats,paint);
複製代碼
點
void drawPoint (float x, float y, Paint paint)
float X:點的X座標
float Y:點的Y座標
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 canvas.drawPoint(100,200,paint);
複製代碼
多個點
void drawPoints (float[] pts, Paint paint)
void drawPoints (float[] pts, int offset, int count, Paint paint)
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 float [] floats ={10,10,100,100,150,150,300,300};
9 canvas.drawPoints(floats,2, 4, paint);
複製代碼
矩形工具類RectF與Rect
RectF:
RectF()
RectF(float left, float top, float right, float bottom)
RectF(RectF r)
RectF(Rect r)
Rect
Rect()
Rect(int left, int top, int right, int bottom)
Rect(Rect r)
圓角矩形
RectF rect:要畫的矩形
float rx:生成圓角的橢圓的X軸半徑
float ry:生成圓角的橢圓的Y軸半徑
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 canvas.drawRect(10,10,100,100,paint);
9
10 RectF rectF = new RectF(120,10,200,100);
11 canvas.drawRect(rectF,paint);
12
13 Rect rect = new Rect(250,10,320,100);
14 canvas.drawRect(rect,paint);
15 }
複製代碼
矩形
void drawRect (float left, float top, float right, float bottom, Paint paint)
void drawRect (RectF rect, Paint paint)
void drawRect (Rect r, Paint paint)
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 RectF rectF = new RectF(200,10,400,100);
9 canvas.drawRoundRect(rectF,20,10,paint);
複製代碼
void drawOval (RectF oval, Paint paint)
RectF oval:用來生成橢圓的矩形
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置填充樣式 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 RectF rectF = new RectF(200,10,400,100);
9 canvas.drawRect(rectF,paint);
10 paint.setColor(Color.RED);
11 canvas.drawOval(rectF,paint);
12 }
複製代碼
弧
void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
RectF oval:生成橢圓的矩形
float startAngle:弧開始的角度,以X軸正方向爲0度
float sweepAngle:弧持續的角度
boolean useCenter:是否有弧的兩邊,True,還兩邊,False,只有一條弧
將畫筆設爲描邊
1 //設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.STROKE);//設置樣式改成描邊 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 RectF rectF = new RectF(200,10,400,100);
9 canvas.drawArc(rectF,0,100,true,paint);
10
11
12 RectF rectF1 = new RectF(400,10,500,100);
13 canvas.drawArc(rectF1,0,80,false,paint);
複製代碼
將畫筆設爲填充
1//設置畫筆基本屬性
2 Paint paint = new Paint();
3
4 paint.setColor(Color.BLUE);//設置畫筆顏色
5 paint.setStyle(Paint.Style.FILL);//設置樣式改成描邊 Style.FILL/Style.FILL_AND_STROKE/Style.STROKE
6 paint.setStrokeWidth(10);//設置畫筆寬度
7
8 RectF rectF = new RectF(200,10,400,100);
9 canvas.drawArc(rectF,0,100,true,paint);
10
11
12 RectF rectF1 = new RectF(400,10,500,100);
13 canvas.drawArc(rectF1,0,80,false,paint);
複製代碼