MainActivitypublic class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
自定义代码
Zidingypublic class Zidingy extends View { private Paint mPaint; Context mContext; public Zidingy(Context context) { super(context); } public Zidingy(Context context, AttributeSet attrs) { super(context, attrs); } public Zidingy(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext=context; } /*测量*/ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /*绘制*/ @Override protected void onDraw(Canvas canvas) { /*画图*/ /*初始化画笔 文字*/ super.onDraw(canvas); Paint pa = new Paint(); /*圆*/ int we=canvas.getWidth()/2; int he=canvas.getHeight()/2; canvas.drawColor(Color.WHITE); pa.setColor(Color.RED); canvas.drawCircle(we,he,200,pa); pa.setColor(Color.WHITE); canvas.drawCircle(we,he,150,pa); pa.setColor(Color.YELLOW); canvas.drawCircle(we,he,100,pa); pa.setColor(Color.BLUE); canvas.drawText("武晓瑞",we-17,he,pa); //------------------------------------------------------------------- // 创建画笔(正方形) Paint p = new Paint(); //设置实心 p.setStyle(Paint.Style.FILL); // 设置红色 p.setColor(Color.BLACK); // 设置画笔的锯齿效果 p.setAntiAlias(true); //绘制// canvas.drawRect(50, 100, 300, 300, p);//(左、上、右、下) canvas.drawRect(10,150,300,300,p);// canvas.drawText("我是你的小可爱",wi-40,hi,pa); //------------------------------------------------------------------- /*三角形*/ Paint p1 = new Paint(); p1.setColor(Color.BLACK); //实例化路径 Path path = new Path(); path.moveTo(80, 200);// 此点为多边形的起点 path.lineTo(120, 250); path.lineTo(80, 250); path.close(); // 使这些点构成封闭的多边形 canvas.drawPath(path, p1); //------------------------------------------------------------------- /*扇形——SectorActivity*/ // 创建画笔 Paint p2 = new Paint(); p2.setColor(Color.RED); RectF rectF = new RectF(60, 100, 200, 240); canvas.drawArc(rectF, 200, 130, true, p2); //------------------------------------------------------------------- /*椭圆——OvalActivity*/ // 创建画笔 Paint p3 = new Paint(); p3.setColor(Color.GREEN); RectF rectF1 = new RectF(60, 100, 200, 240); rectF1.set(210,100,250,130); canvas.drawOval(rectF1, p3); } /*定位*/ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } /*监听事件*/ @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } public Zidingy(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }}布局代码xml文件<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bwie.test.huayuan.MainActivity"> <com.bwie.test.huayuan.Zidingy android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>