参考: http://blog.youkuaiyun.com/lee576/article/details/7860935
新建一个view 文件 , DiyView.java
package com.test1;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* 九宫格解锁控件
* Created by Jerry on 2015/9/21.
*/
public class DiyView extends View {
public DiyView(Context context) {
this(context, null);
}
public DiyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DiyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.RED);
//新建画笔
Paint paint = new Paint();
//给画笔设置属性
paint.setAntiAlias(true);//消除据此
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
//画直线
//drawLine(float startX, float startY, float stopX, float stopY,Paint)
canvas.drawLine(10,10,100,10,paint);
//通过画笔绘制圆形
//画圆
canvas.drawCircle(40, 40, 30, paint);
//绘制正方形
canvas.drawRect(10, 80, 70, 140, paint);
//绘制矩形
canvas.drawRect(10, 150, 70, 190, paint);
//绘制圆角矩形
RectF re1 = new RectF(10,200, 70, 230);
canvas.drawRoundRect(re1, 15, 15, paint);
RectF rel1 = new RectF(10,240,70,270);
//绘制椭圆
canvas.drawOval(rel1, paint);
//定义一个Path对象,封闭成一个三角形
Path path1 = new Path();
path1.moveTo(10, 340);
path1.lineTo(70, 340);
path1.lineTo(40, 290);
path1.close();
//根据path进行绘制,绘制三角形
canvas.drawPath(path1,paint);
//根据path进行绘制,封闭成一个五角形
Path path2 = new Path();
path2.moveTo(26, 360);
path2.lineTo(54, 360);
path2.lineTo(70, 392);
path2.lineTo(40, 420);
path2.lineTo(10, 392);
path2.close();
//根据path进行绘制,绘制五角形
canvas.drawPath(path2,paint);
//-----------------设置填充风格后绘制------------------
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawCircle(120, 40, 30, paint);
//绘制正方形
canvas.drawRect(90, 80, 150, 140, paint);
//绘制矩形
canvas.drawRect(90, 150, 150, 190, paint);
RectF re2 = new RectF(90,200,150,230);
//绘制圆角矩形
canvas.drawRoundRect(re2, 15, 15, paint);
RectF re21 = new RectF(90,240,150,270);
//绘制椭圆
canvas.drawOval(re21, paint);
Path path3 = new Path();
path3.moveTo(90, 340);
path3.lineTo(150, 340);
path3.lineTo(120, 290);
path3.close();
//绘制三角形
canvas.drawPath(path3, paint);
Path path4 = new Path();
path4.moveTo(106, 360);
path4.lineTo(134, 360);
path4.lineTo(150, 392);
path4.lineTo(120, 420);
path4.lineTo(90, 392);
path4.close();
//绘制五角形
canvas.drawPath(path4, paint);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="物竞天择 适者生存" /> <com.test1.DiyView android:id="@+id/lockView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#2B2B2B" /> </RelativeLayout>
MainActivity.java
package com.test1;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
}
}