Paint paint=new Paint();//创建一个采用默认设置的画笔 paint.setAntiAlias(true);//使用抗锯齿的功能 paint.setColor(Color.RED);//设置颜色为红色 paint.setStrokeWidth(2);//笔触的宽度为2像素 paint.setStyle(Style.STROKE);//填充样式为描边(实心是Style.FILL)
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的帧布局管理器 FrameLayout ll=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 ll.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE);//指定画布的背景颜色为白色 Paint paint=new Paint();//创建采用默认的画笔 paint.setAntiAlias(true);//使用抗锯齿功能 paint.setStrokeWidth(3);//设置笔触的宽度 paint.setStyle(Style.STROKE);//设置填充样式为描边 paint.setColor(Color.BLUE);//设置画笔颜色 canvas.drawCircle(50, 50, 30, paint);//绘制蓝色的圆形 paint.setColor(Color.YELLOW);//设置画笔颜色 canvas.drawCircle(100, 50, 30, paint);//绘制黄色的圆形 paint.setColor(Color.BLACK);//设置画笔颜色 canvas.drawCircle(150, 50, 30, paint);//绘制黑色的圆形 paint.setColor(Color.GREEN);//设置画笔颜色 canvas.drawCircle(75, 90, 30, paint);//绘制绿色的圆形 paint.setColor(Color.RED);//设置画笔颜色 canvas.drawCircle(125, 90, 30, paint);//绘制红色的圆形 super.onDraw(canvas); } } }
效果如图
转载请注明出处:http://blog.csdn.net/acmman/article/details/45177289