Android自定义控件绘制基本图形基础入门
本文讲述绘制Android自定义各种图形效果,为自定义控件的入门篇
相关视频链接:
Android自定义控件系列
http://edu.csdn.net/course/detail/3719/65396
Android视频全系列
http://edu.csdn.net/course/detail/2741/43163
绘制点–这个控件只需要在布局中引用或者代码中new即可,下面几个绘制只展示onDraw方法
packagecom.example.viewdemo1.view;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Paint.Style;
importandroid.util.AttributeSet;
importandroid.view.View;
publicclassPointViewextendsView{
publicPointView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
}
publicPointView(Contextcontext,AttributeSetattrs){
super(context,attrs);
}
publicPointView(Contextcontext){
super(context);
}
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
//对于画笔
Paintpaint=newPaint();
//设置抗锯齿
paint.setAntiAlias(true);
//设置画笔颜色
paint.setColor(Color.RED);
//三种样式
paint.setStyle(Style.FILL_AND_STROKE);
paint.setStrokeWidth(5);
//阴影
paint.setShadowLayer(10,0,0,Color.CYAN);
//点的坐标x0,y0,x1,y1......
float[]pts={50,50,100,100,200,200,300,300,0,100,100,0};
canvas.drawPoints(pts,paint);
//绘制点的时候,隔着几个点绘制几个,最多不到多少点
canvas.drawPoints(pts,1,6,paint);
}
}
绘制线
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
//对于画笔
Paintpaint=newPaint();
//设置抗锯齿
paint.setAntiAlias(true);
//设置画笔颜色
paint.setColor(Color.RED);
//三种样式
paint.setStyle(Style.FILL);
paint.setStrokeWidth(0.5f);
//阴影
//paint.setShadowLayer(10,0,0,Color.CYAN);
//x0,y0,x1,y1
float[]pts={100,100,200,200,200,200,300,200,300,200,300,
400};
//以上是6个点的x,y坐标,两两连成线段
canvas.drawLines(pts,paint);
//画一条线
canvas.drawLine(0,0,100,100,paint);
}
绘制圆
//指定圆心坐标,半径就OK canvas.drawCircle(100,100,100,paint);
绘制文字
//设置文字大小
paint.setTextSize(40);
//指定坐标,最好指定文字大小
canvas.drawText("哈",100,500,paint);
//将文字设置到指定路径上
Pathpath=newPath();
paint.setTextSize(50);
path.addCircle(200,200,150,Direction.CCW);
canvas.drawTextOnPath("我爱你我的祖国,我爱你我亲爱的姑娘",path,0,0,paint);
绘制矩形
//阴影 paint.setShadowLayer(10,0,0,Color.CYAN); //xy坐标及半径值 //canvas.drawCircle(100,100,50,paint); canvas.drawRect(50,50,300,300,paint);
绘制圆弧
//指定放置圆弧的矩形 RectFoval=newRectF(10,10,210,210); //绘制圆弧-0是指开始度数,270是指结束度数false是指不连接圆心,paint是画笔 canvas.drawArc(oval,0,270,false,paint);
绘制椭圆
//指定矩形,指定画笔 canvas.drawOval(oval,paint);
以上就是基本图形的绘制了…veryeasy。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。