onMeasure:用于测量视图的大小;
onLayout:用于给视图进行布局;
onDraw:用于对视图进行绘制;
相应的思维导图如下:
setARGB(int a, int r, int g, int b) 用于设置颜色,各参数值均为0~255之间的整数,分别用于表示透明度、红色、绿色和蓝色值
setColor(int color) 用于设置颜色,参数color可以通过Color类提供的颜色常量指定
3.Color.rgb(in t red,int green,int blue)方法指定颜色
setAlpha(int a) 用于设置透明度,值为0~255之间的整数
setAntiAlias(boolean aa) 用于指定是否使用抗锯齿功能,如果使用会使绘图速度变慢 ,但是一般图像绘制都会设置使用。
6.setDither(boolean dither) 用于指定是否使用图像抖动处理,如果使用会使图像颜色更加平滑和饱满,更加清晰
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的基本设置函数:
-
paint.setAntiAlias(true);//抗锯齿功能
-
paint.setColor(Color.RED); //设置画笔颜色
-
paint.setStyle(Style.FILL);//设置填充样式
-
paint.setStrokeWidth(30);//设置画笔宽度
-
paint.setShadowLayer(10, 15, 15, Color.GREEN);//设置阴影
-
1、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);//画图
TIM图片20180818185340.png
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);
TIM图片20180818190727.png
多条直线
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);
TIM图片20180818191011.png
点
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);
TIM图片20180818192510.png
多个点
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);
TIM图片20180818192255.png
矩形工具类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 }
TIM图片20180818194820.png
矩形
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);
TIM图片20180818194340.png
椭圆
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 }
TIM图片20180818195749.png
弧
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);
![TIM图片20180818214246.png](https://img.haomeiwen.com/i9882464/8f70c1379908e8b2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
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);
TIM图片20180818214548.png
网友评论