首页 技术 正文
技术 2022年11月8日
0 收藏 698 点赞 1,700 浏览 1697 个字

Canvas,Paint

1.在android 绘图但中经常要用到Canvas和Paint类,Canvas好比是一张画布,上面已经有你想绘制图画的轮廓了,而Paint就好比是画笔,就要给Canvas进行添色等操作。

这两个类通常都是在onDraw(Canvas canvas)方法中用的。

2.Bitmap:代表一张位图,BitmapDrawable里封装的突变就是一个Bitmao对象

3.Canvas里面有一些例如:

drawArc(参数) 绘制弧

drawBitmao(Bitmap bitmap ,Rect rect,Rect dst,Paint paint)  在指定点绘制从源图中”挖取”的一块

clipRect(float left,float top,float right,float bottom)  剪切一个矩形区域

clipRegion(Region region)  剪切一个指定区域。

Canvas除了直接绘制一个基本图形外,还提供了如下方法进行坐标变化:

rotate(float degree,float px, float py):对Canvas执行旋转变化

scale(float sx,float sy,float px,float py):对Cnavas进行缩放变换

skew(float sx,float sy):对Canvas执行倾斜变换

translate(float dx,float dy):对Cnavas执行移动

4.Paint类主要用于设置绘制风格包括画笔颜色,画笔粗细,填充风格等,

Paint提供了一些方法

setARGB(int a,int r,int g,int b)/setColor(int color)  :设置颜色

等一些方法

5.下面通过一个例子来说明一下这两个类:

 public class MyView extends View {     public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
} public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
} public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
} // 重写该方法,进行绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 整张画布绘制成白色
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
// 去锯齿
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
// 绘制图形
canvas.drawCircle(40, 40, 30, paint);
// 绘制正方型
canvas.drawRect(10, 80, 70, 140, paint);
// 绘制矩形
canvas.drawRect(10, 150, 70, 190, paint);
RectF rel = new RectF(10, 200, 70, 230);
// 绘制圆角矩形
canvas.drawRoundRect(rel, 15, 15, paint);
RectF rell = new RectF(10, 240, 70, 270);
// 绘制椭圆
canvas.drawOval(rell, paint);
// 定义一个Path对象,封闭成一个三角形
Path path1 = new Path();
path1.moveTo(10, 340);
path1.lineTo(70, 340);
path1.lineTo(40, 290);
path1.close(); } }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892