Android学习笔记绘制几何图形
作者:互联网
案例绘制一个安卓机器人
activity_main.xml布局就一个frameLayout,省略掉
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取帧布局管理器
FrameLayout frameLayout = findViewById(R.id.frameLayout);
frameLayout.addView(new MyView(this));//将自定义View添加到布局管理器中
}
MyView.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;
public class MyView extends View {
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**绘制Android机器人**/
Paint paint = new Paint();//创建一个画笔
paint.setAntiAlias(true);//采用锯齿功能
paint.setColor(0xFFA4C739);//设置画笔的颜色为绿色
//绘制机器人的头
RectF rectF = new RectF(10,10,100,100);//定义外轮廓矩形
rectF.offset(90,20);
canvas.drawArc(rectF,-10,-160,false,paint);//绘制弧形
//绘制眼睛
paint.setColor(0xFFFFFFFF);//设置画笔为白色
canvas.drawCircle(165,53,4,paint);//绘制圆
canvas.drawCircle(125,53,4,paint);//绘制圆
//绘制天线
paint.setColor(0xFFA4C739);
paint.setStrokeWidth(2);//设置笔触的宽度
canvas.drawLine(110,15,
125,35,paint);
canvas.drawLine(180,15,
165,35,paint);//绘制线
//绘制身体
canvas.drawRect(100,75,
190,150,paint);
RectF rectF_body = new RectF(100,140,
190,160);
canvas.drawRoundRect(rectF_body,10,
10,paint);//绘制圆角矩形
//绘制胳膊
RectF rectF_arm = new RectF(75,75,
95,140);
canvas.drawRoundRect(rectF_arm,10,
10,paint);//绘制圆角矩形
rectF_arm.offset(120,0);
canvas.drawRoundRect(rectF_arm,10,10,paint);//绘制圆角矩形
//绘制腿
RectF rectF_leg = new RectF(115,150,
135,200);
canvas.drawRoundRect(rectF_leg,10,10,paint);
rectF_leg.offset(40,0);
canvas.drawRoundRect(rectF_leg,10,10,paint);
}
}
效果:
标签:10,canvas,几何图形,rectF,笔记,RectF,paint,Android,绘制 来源: https://www.cnblogs.com/lzpq/p/13157512.html