Android 学习笔记(七):绘制基本图形
基础中的基础,代码中有详细解释,
[java] public class DrawingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//XML布局时用setContentView(int layoutResID)
//还有 setContentView(View view)
//和 setContentView(View view, ViewGroup.LayoutParams params)
setContentView(new Drawing(this));
}
}
//新建一个类Drawing继承自View
class Drawing extends View{
//创建一个画笔
Paint paint=new Paint();
//构造方法
public Drawing(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
canvas.drawColor(Color.GRAY);
//设置画笔颜色
paint.setColor(Color.CYAN);
paint.setTextSize(16);
//文本
canvas.drawText("绘制文本", 120, 20, paint);
//直线
canvas.drawLine(120, 40, 220, 40, paint);
//矩形
canvas.drawRect(100, 80, 200, 120, paint);
//圆形 www.zzzyk.com
canvas.drawCircle(90, 200, 30, paint);
//绘制一个椭圆
canvas.drawOval(new RectF(120,200,220,260), paint);
//路径
Path p=new Path();
p.moveTo(50, 0);
p.lineTo(25, 100);
p.lineTo(100, 40);
p.lineTo(0, 40);
p.lineTo(75, 100);
p.lineTo(50, 0);
canvas.drawPath(p, paint);
}
}
摘自 柒尐狐的专栏
补充:移动开发 , Android ,