在Android中,绘制图像时常用的就是Paint类和Canvas类,
其中,Paint类用作画笔,描述图形线宽、颜色等信息,常用属性有(具体不展开):
setColor(int color)设置颜色
setAlpha(int a)设置透明度0-255
setAntiAlias(boolean aa)设置抗锯齿功能
setDither(boolean)设置抖动处理功能
setShader(Shader shader)设置渐变,含Linear、Radial、SweepGradient线性、径向、角度渐变
setStrokeWidth(float width)设置笔触宽度
setTextSize()设置文字大小
setTextAlign()设置文本对齐方式
setXfermode()设置图形重叠时的处理方式、常用于擦除效果
Canvas类作为画布,常用构造方法
在主函数中添加一个内部类
public class MyView extends View{
public MyView(Context context) {
super(context);
}
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
protected void onDraw(Canvas canvas) {
//画笔Paint类常在此处定义。
super.onDraw(canvas);
}
}
}
}
在定义完Paint类用,可绘制相应图像
绘制几何图形:drawArc圆弧,drawCircle圆形,drawLine线,drawPoint点等
绘制文本:drawText在指定位置绘制文字,drawPosText在指定位置绘制文字(每个文字都得指定位置)
绘制路径:addArc、addCircle、addOval椭圆、addRect矩形、moveTo起始点、close闭合路径、lineTo直线
绘制图片:drawBitmap用于在指定位置绘制图形,或在指定位置挖去图形
例1:绘制安卓机器人(Activity.java中的代码)
public class AndroidIco extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout ll=(FrameLayout)findViewById(R.id.frameLayout1); //创建帧布局
ll.addView(new MyView(this)); //添加视图
}
public class MyView extends View{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FrameLayout ll=(FrameLayout)findViewById(R.id.frameLayout1); //创建帧布局
ll.addView(new MyView(this)); //添加视图
}
public class MyView extends View{
public MyView(Context context) {
super(context);
}
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint(); //创建画笔
paint.setAntiAlias(true); //抗锯齿
paint.setColor(0xFFA4C739); //颜色
//绘制头部
RectF rectf_head=new RectF(10, 10, 100, 100);
rectf_head.offset(100, 20);
canvas.drawArc(rectf_head, -10, -160, false, paint);
//眼睛
paint.setColor(Color.WHITE);
canvas.drawCircle(135, 53, 4, paint);
canvas.drawCircle(175, 53, 4, paint);
paint.setColor(0xFFA4C739);
//天线
paint.setStrokeWidth(2);
canvas.drawLine(120, 15, 135, 35, paint);
canvas.drawLine(190, 15, 175, 35, paint);
//身体
canvas.drawRect(110, 75, 200, 150, paint);
RectF rectf_body=new RectF(110,140,200,160);
canvas.drawRoundRect(rectf_body, 10, 10, paint);
//胳膊
RectF rectf_arm=new RectF(85,75,105,140);
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
rectf_arm.offset(120, 0); //x轴上偏移像素120
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
//腿部
RectF rectf_leg=new RectF(125,150,145,200);
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
rectf_leg.offset(40, 0);
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
super.onDraw(canvas);
}
}
}
protected void onDraw(Canvas canvas) {
Paint paint=new Paint(); //创建画笔
paint.setAntiAlias(true); //抗锯齿
paint.setColor(0xFFA4C739); //颜色
//绘制头部
RectF rectf_head=new RectF(10, 10, 100, 100);
rectf_head.offset(100, 20);
canvas.drawArc(rectf_head, -10, -160, false, paint);
//眼睛
paint.setColor(Color.WHITE);
canvas.drawCircle(135, 53, 4, paint);
canvas.drawCircle(175, 53, 4, paint);
paint.setColor(0xFFA4C739);
//天线
paint.setStrokeWidth(2);
canvas.drawLine(120, 15, 135, 35, paint);
canvas.drawLine(190, 15, 175, 35, paint);
//身体
canvas.drawRect(110, 75, 200, 150, paint);
RectF rectf_body=new RectF(110,140,200,160);
canvas.drawRoundRect(rectf_body, 10, 10, paint);
//胳膊
RectF rectf_arm=new RectF(85,75,105,140);
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
rectf_arm.offset(120, 0); //x轴上偏移像素120
canvas.drawRoundRect(rectf_arm, 10, 10, paint);
//腿部
RectF rectf_leg=new RectF(125,150,145,200);
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
rectf_leg.offset(40, 0);
canvas.drawRoundRect(rectf_leg, 10, 10, paint);
super.onDraw(canvas);
}
}
}