自定义绘制View
2016-05-26 23:05
363 查看
Paint(画笔)
Canvas(画布)
TheCanvasclassholdsthe"draw"calls.
Todrawsomething,youneed4basiccomponents:ABitmaptoholdthepixels,aCanvastohostthedrawcalls(writingintothebitmap),adrawingprimitive(e.g.Rect,Path,text,Bitmap),andapaint(todescribethecolorsandstylesforthedrawing).
rotate(floatdegrees,floatpx,floatpy)//对canvas执行旋转变换
//
setBitmap(Bitmapbitmap)
Path(绘画路径)
预先在View上将N个点连成一条“路径”,然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形
Android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含如下子类:
ComposePathEffectCornerPathEffectDashPathEffectDiscretePathEffectPathDashPathEffectSumPathEffect
Paintpaint=newPaint();
实例:采用双缓冲实现画图板
整理自:《疯狂Android讲义》
来自为知笔记(Wiz)
Canvas(画布)
TheCanvasclassholdsthe"draw"calls.
Todrawsomething,youneed4basiccomponents:ABitmaptoholdthepixels,aCanvastohostthedrawcalls(writingintothebitmap),adrawingprimitive(e.g.Rect,Path,text,Bitmap),andapaint(todescribethecolorsandstylesforthedrawing).
rotate(floatdegrees,floatpx,floatpy)//对canvas执行旋转变换
scale(floatsx,floatsy)//对canvas执行缩放
skew(floatsx,floatsy)//对canvas执行倾斜变换
translate(floatdx,floatdy)//移动canvas,向右dx,向下dy
//
setBitmap(Bitmapbitmap)
Path(绘画路径)
预先在View上将N个点连成一条“路径”,然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形
//Path
moveTo(floatx,floaty)//Setthebeginningofthenextcontourtothepoint(x,y).
lineTo(floatx,floaty)//Addalinefromthelastpointtothespecifiedpoint(x,y).
//canvas
drawTextOnPath(Stringtext,Pathpath,floathOffset,floatvOffset,Paintpaint)//hOffset水平偏移vOffset垂直偏移
Android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含如下子类:
ComposePathEffectCornerPathEffectDashPathEffectDiscretePathEffectPathDashPathEffectSumPathEffect
Paintpaint=newPaint();
paint.setPathEffect(PathEffecteffect);
实例:采用双缓冲实现画图板
publicclassDrawViewextendsView
{
//定义记录前一个拖动事件发生点的坐标
floatpreX;
floatpreY;
privatePathpath;
publicPaintpaint=null;
//定义一个内存中的图片,该图片将作为缓冲区
BitmapcacheBitmap=null;
//定义cacheBitmap上的Canvas对象
CanvascacheCanvas=null;
publicDrawView(Contextcontext,intwidth,intheight)
{
super(context);
//创建一个与该View相同大小的缓存区
cacheBitmap=Bitmap.createBitmap(width,height,
Bitmap.Config.ARGB_8888);
cacheCanvas=newCanvas();
path=newPath();
//设置cacheCanvas将会绘制到内存中的cacheBitmap上
cacheCanvas.setBitmap(cacheBitmap);
//设置画笔的颜色
paint=newPaint(Paint.DITHER_FLAG);
paint.setColor(Color.RED);
//设置画笔风格
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
//反锯齿
paint.setAntiAlias(true);
paint.setDither(true);
}
@Override
publicbooleanonTouchEvent(MotionEventevent)
{
//获取拖动事件的发生位置
floatx=event.getX();
floaty=event.getY();
switch(event.getAction())
{
caseMotionEvent.ACTION_DOWN:
//从前一个点绘制到当前点之后,把当前点定义成下次绘制的前一个点
path.moveTo(x,y);
preX=x;
preY=y;
break;
caseMotionEvent.ACTION_MOVE:
//从前一个点绘制到当前点之后,把当前点定义成下次绘制的前一个点
path.quadTo(preX,preY,x,y);
preX=x;
preY=y;
break;
caseMotionEvent.ACTION_UP:
cacheCanvas.drawPath(path,paint);//①
path.reset();
break;
}
invalidate();
//返回true表明处理方法已经处理该事件
returntrue;
}
@Override
publicvoidonDraw(Canvascanvas)
{
PaintbmpPaint=newPaint();
//将cacheBitmap绘制到该View组件上
canvas.drawBitmap(cacheBitmap,0,0,bmpPaint);//②
//沿着path绘制
canvas.drawPath(path,paint);
}
}
整理自:《疯狂Android讲义》
相关文章推荐
- 160_复杂界面动画变化
- Android 实现简单媒体播放器功能
- 【线段树】ZKW线段树(noip2012D2T2)(选教室)
- VS2015的头文件加载
- 运用block,防止循环引用
- 159_平滑移动动画
- 记MFC自绘ListBox闪烁处理
- 读懂diff
- Iwfu-ToolBar
- iOS开发--遇到的友盟的几个坑
- HDU2516 取石子游戏(fibonacci博弈)
- 欢迎使用CSDN-markdown编辑器
- 第二阶段冲刺(七)
- GCD及多线程
- 157_传递拖拽事件
- CRF分词的java实现
- 【Spring4揭秘 基础2】PropertySource和Enviroment
- 通过一张图片定位真实地址
- [leetcode] 344. Reverse String && 345. Reverse Vowels of a String
- 进程间通信之信号量