您的位置:首页 > 大数据 > 人工智能

Canvas(画布)、Paint(画笔) 详解

2016-03-30 16:43 591 查看
一、自定义控件分类:

1.组合控件:将系统原生控件组合起来,加上动画效果,形成一种特殊的UI效果

2.纯粹自定义控件:继承自系统的View,自己去实现view效果

二、Canvas(画布)、Paint(画笔) 详解

在Android中需要通过graphics类来显示2D图形。graphics中包括了Canvas(画布)、Paint(画笔)、Color(颜色)、Bitmap(图像)等常用的类。graphics具有绘制点、线、颜色、2D几何图形、图像处理等功能。

1.Canvas:介绍

1.void drawCircle (float cx, float cy, float radius, Paint paint)

cx:圆心的x坐标            cy:圆心的y坐标。

radius:圆的半径           paint:绘制时所使用的画笔。
2.void drawRect (float left, float top, float right, float bottom, Paint paint)
 void drawRect (RectF rect, Paint paint)
 void drawRect (Rect r, Paint paint)

第一个的写法是直接传入矩形的四个点,画出矩形
第二、三个构造函数是根据传入RectF或者Rect矩形变量来指定所画的矩形的          
①矩形工具类RectF与Rect
这两个都是矩形辅助类,区别不大,用哪个都行,根据四个点构建一个矩形结构;在画图时,
利用这个矩形结构可以画出对应的矩形或者与其它图形Region相交、相加等等;   
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)

3.void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
RectF rect:要画的矩形
float rx:生成圆角的椭圆的X轴半径

float ry:生成圆角的椭圆的Y轴半径

4.void drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象

Path对象的方法
reset()    //reset()清除path设置的所有属性                        
lineTo(float x, float y) //lineTo(float x, float y)方法用于从当前轮廓点绘制一条线段到x,y点:     
moveTo(float x, float y) //path的moveTo方法将起始轮廓点移至x,y坐标点,默认情况为0,0点
close() //回到初始点形成封闭的曲线
path.addArc(oval, startAngle, sweepAngle)
path.addArc方法用于绘制圆弧,这个圆弧取自RectF矩形的内接椭圆上的一部分,圆弧长度由后两个参数决定
startAngle:起始位置的角度值
sweepAngle:旋转的角度值
arcTo(RectF oval, float startAngle, float sweepAngle)
arcTo和addArc的区别:

      1. addArc可以直接加入一段椭圆弧。使用arcTo还需要使用moveTo指定当前点的坐标。

      2. arcTo如果当前点坐标和曲线的起始点不是同一个点的话,还会自动添加一条直线补齐路径。
quadTo(float x1, float y1, float x2, float y2)//以当前路径结束点为开始点,(x1,y1)为控制点,(x2,y2)为结束点画
一条二次贝塞尔曲线
addCircle(float x, float y, float radius, Direction dir)

    使用path绘制圆形,xy为圆的圆心 radius为圆的半径,Direction 为绘制元的方向

    Diection.CCW 逆时针方向

    Diection.CW 顺时针方向
addOval(RectF oval, Path.Direction dir)//绘制椭圆
addPath(Path src, float dx, float dy)//在已有的Path上通过平移创建新的path:
5.void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
参数一就是我们常规的Bitmap对象

参数二是源区域(这里是bitmap)

参数三是目标区域(应该在 canvas的位置和大小),
参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。     
6. void drawLine(float startX, float startY, float stopX, float stopY, Paint paint)
画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,
最后一个参数为Paint画刷对象。     
7.void drawPoint(float x, float y, Paint paint)
画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。
 8.void drawText(String text, float x, float y, Paint paint)
渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数
四是Paint对象。   
9.void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)
在路径上绘制文本,相对于上面第二个参数是Path路径对象

2.Paint:简介
void setStyle(Style.STROKE);

设置画笔的样式:Style.FILL: 实心   STROKE:空心   FILL_OR_STROKE:同时实心与空心
void setStrokeWidth(10);

当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的粗细度 
void setARGB(int a, int r, int g, int b) 设置Paint对象颜色,参数一为alpha透明通道
void setAlpha(int a) 设置alpha不透明度,范围为0~255
void setAntiAlias(boolean aa) //是否抗锯齿
void setColor(int color) //设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义
void setFakeBoldText(boolean fakeBoldText) //设置伪粗体文本
void setLinearText(boolean linearText)
//设置线性文本
PathEffect setPathEffect(PathEffect effect)
//设置路径效果
Rasterizer setRasterizer(Rasterizer rasterizer) //设置光栅化
Shader setShader(Shader shader) //设置阴影
void setTextAlign(Paint.Align align) //设置文本对齐
void setTextScaleX(float scaleX) //设置文本缩放倍数,1.0f为原始
void setTextSize(float textSize) //设置字体大小
Typeface setTypeface(Typeface typeface)设置字体,Typeface包含了字体的类型,粗细,还有倾斜、颜色等。

 void setUnderlineText(boolean underlineText)
//设置下划线

 三、代码实现

package myview;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.example.myview_day01.R;

/**
* Created by Administrator on 2016/7/5 0005.
*/
public class MyView extends View {
public MyView(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint_blue = new Paint(); // 绘制蓝色的环
paint_blue.setColor(Color.BLUE);
paint_blue.setStyle(Paint.Style.FILL);
paint_blue.setStrokeWidth(10);
canvas.drawCircle(110, 150, 80, paint_blue);

Paint paint_yellow = new Paint(); // 绘制黄色的环
paint_yellow.setColor(Color.YELLOW);
paint_yellow.setStyle(Paint.Style.STROKE);
paint_yellow.setStrokeWidth(10);
canvas.drawCircle((float) 175.5, 210, 80, paint_yellow);

Paint paint_black = new Paint(); // 绘制黑色的环
paint_black.setColor(Color.BLACK);
paint_black.setStyle(Paint.Style.STROKE);
paint_black.setStrokeWidth(10);
canvas.drawCircle(245, 150, 80, paint_black);

Paint paint_green = new Paint(); // 绘制绿色的环
paint_green.setColor(Color.GREEN);
paint_green.setStyle(Paint.Style.STROKE);
paint_green.setStrokeWidth(10);
canvas.drawCircle(311, 210, 80, paint_green);

Paint paint_red = new Paint(); // 绘制红色的环
paint_red.setColor(Color.RED);
paint_red.setStyle(Paint.Style.STROKE);
paint_red.setStrokeWidth(10);
canvas.drawCircle(380, 150, 80, paint_red);

Paint paint_string = new Paint(); // 绘制字符串
paint_string.setColor(Color.BLUE);
paint_string.setTextSize(20);
canvas.drawText("Welcome to Beijing", 245, 310, paint_string);

Paint paint_line = new Paint(); // 绘制直线
paint_line.setColor(Color.BLUE);
canvas.drawLine(240, 310, 425, 310, paint_line);

Paint paint_text = new Paint(); // 绘制字符串
paint_text.setColor(Color.BLUE);
paint_text.setTextSize(20);
canvas.drawText("北京欢迎您", 275, 330, paint_text);

Paint rec_paint =new Paint();//绘制正方形区域
rec_paint.setColor(Color.GREEN);
RectF rect= new RectF(230, 400, 330, 500);
canvas.drawRect(rect, rec_paint);

Paint rund_paint =new Paint();//绘制圆角正方形区域
rund_paint.setColor(Color.RED);
RectF rund_rect= new RectF(230, 510, 330, 610);
canvas.drawRoundRect(rund_rect, 100, 100, rund_paint);

//绘制福娃图片
//canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.dudu), 200, 800, paint_line);

}

}
四、效果图



 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: