看效果:

直接看代码
package com.example.study_05_view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
public class AttrsView extends View {
Paint paint;
Paint paint2;
Paint paint3;
Paint paint4;
int myWid;
int myHie;
Path path;
Path path2;
Path path3;
Path path4;
Context context;
int height;
int width;
public AttrsView(Context context,AttributeSet attrs) {
super(context, attrs);
this.context=context;
//AttributeSet 对应Set集合
//获取自定义属性集合
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AttrsView);
myHie= typedArray.getInteger(R.styleable.AttrsView_hei,0);
myWid = typedArray.getInteger(R.styleable.AttrsView_wid,0);
paint=new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint2=new Paint();
paint2.setStyle(Paint.Style.STROKE);
paint2.setColor(Color.YELLOW);
paint2.setStrokeWidth(2);
paint2.setAntiAlias(true);
paint3=new Paint();
paint3.setStyle(Paint.Style.STROKE);
paint3.setColor(Color.GREEN);
paint3.setStrokeWidth(2);
paint3.setAntiAlias(true);
paint4=new Paint();
paint4.setStyle(Paint.Style.STROKE);
paint4.setColor(Color.BLUE);
paint4.setStrokeWidth(2);
paint4.setAntiAlias(true);
//获取窗口
WindowManager windowManager= (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
//
DisplayMetrics displayMetrics=new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(displayMetrics);
height=displayMetrics.heightPixels;
width= displayMetrics.widthPixels;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path=new Path();
path.moveTo(0,0);
path.lineTo(width/2,height/2);
path.lineTo(width,0);
path2=new Path();
path2.moveTo(width,0);
path2.lineTo(width/2,height/2);
path2.lineTo(width,height);
path3=new Path();
path3.moveTo(width,height);
path3.lineTo(0,height);
path3.lineTo(width/2,height/2);
path4=new Path();
path4.moveTo(0,height);
path4.lineTo(0,0);
path4.lineTo(width/2,height/2);
canvas.drawPath(path,paint);
canvas.drawPath(path2,paint2);
canvas.drawPath(path3,paint3);
canvas.drawPath(path4,paint4);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x =(int) event.getX();
int y =(int) event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
RectF rectF = new RectF();
//分割矩形
path.computeBounds(rectF,true);
//一个区域
Region region = new Region();
region.setPath(path, new Region((int)rectF.left, (int)rectF.top, (int)rectF.right,(int) rectF.bottom));
boolean contains = region.contains(x, y);
if(contains){
paint.setStyle(Paint.Style.FILL);
}else{
paint.setStyle(Paint.Style.STROKE);
}
RectF rectF2 = new RectF();
//分割矩形
path2.computeBounds(rectF2,true);
//一个区域
Region region2 = new Region();
region2.setPath(path2, new Region((int)rectF2.left, (int)rectF2.top, (int)rectF2.right,(int) rectF2.bottom));
boolean contains2 = region2.contains(x, y);
if(contains2){
paint2.setStyle(Paint.Style.FILL);
}else{
paint2.setStyle(Paint.Style.STROKE);
}
RectF rectF3 = new RectF();
//分割矩形
path3.computeBounds(rectF3,true);
//一个区域
Region region3 = new Region();
region3.setPath(path3, new Region((int)rectF3.left, (int)rectF3.top, (int)rectF3.right,(int) rectF3.bottom));
boolean contains3 = region3.contains(x, y);
if(contains3){
paint3.setStyle(Paint.Style.FILL);
}else{
paint3.setStyle(Paint.Style.STROKE);
}
RectF rectF4 = new RectF();
//分割矩形
path4.computeBounds(rectF4,true);
//一个区域
Region region4 = new Region();
region4.setPath(path4, new Region((int)rectF4.left, (int)rectF4.top, (int)rectF4.right,(int) rectF4.bottom));
boolean contains4 = region4.contains(x, y);
if(contains4){
paint4.setStyle(Paint.Style.FILL);
}else{
paint4.setStyle(Paint.Style.STROKE);
}
break;
}
invalidate();
return true;
}
}